Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver. |
| 3 | * |
| 4 | * Maintainer: Alan Stern <stern@rowland.harvard.edu> |
| 5 | * |
| 6 | * Copyright (C) 2003 David Brownell |
| 7 | * Copyright (C) 2003-2005 Alan Stern |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | |
| 16 | /* |
| 17 | * This exposes a device side "USB gadget" API, driven by requests to a |
| 18 | * Linux-USB host controller driver. USB traffic is simulated; there's |
| 19 | * no need for USB hardware. Use this with two other drivers: |
| 20 | * |
| 21 | * - Gadget driver, responding to requests (slave); |
| 22 | * - Host-side device driver, as already familiar in Linux. |
| 23 | * |
| 24 | * Having this all in one kernel can help some stages of development, |
| 25 | * bypassing some hardware (and driver) issues. UML could help too. |
| 26 | */ |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/errno.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/timer.h> |
| 36 | #include <linux/list.h> |
| 37 | #include <linux/interrupt.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 38 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/usb.h> |
David Brownell | 9454a57 | 2007-10-04 18:05:17 -0700 | [diff] [blame] | 40 | #include <linux/usb/gadget.h> |
Eric Lescouet | 27729aa | 2010-04-24 23:21:52 +0200 | [diff] [blame] | 41 | #include <linux/usb/hcd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include <asm/byteorder.h> |
| 44 | #include <asm/io.h> |
| 45 | #include <asm/irq.h> |
| 46 | #include <asm/system.h> |
| 47 | #include <asm/unaligned.h> |
| 48 | |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #define DRIVER_DESC "USB Host+Gadget Emulator" |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 51 | #define DRIVER_VERSION "02 May 2005" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Alan Stern | caf29f6 | 2007-12-06 11:10:39 -0500 | [diff] [blame] | 53 | #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */ |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | static const char driver_name [] = "dummy_hcd"; |
| 56 | static const char driver_desc [] = "USB Host+Gadget Emulator"; |
| 57 | |
| 58 | static const char gadget_name [] = "dummy_udc"; |
| 59 | |
| 60 | MODULE_DESCRIPTION (DRIVER_DESC); |
| 61 | MODULE_AUTHOR ("David Brownell"); |
| 62 | MODULE_LICENSE ("GPL"); |
| 63 | |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 64 | struct dummy_hcd_module_parameters { |
| 65 | bool is_super_speed; |
Tatyana Brokhman | 7eca4c5 | 2011-06-29 16:41:53 +0300 | [diff] [blame] | 66 | bool is_high_speed; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static struct dummy_hcd_module_parameters mod_data = { |
Tatyana Brokhman | 7eca4c5 | 2011-06-29 16:41:53 +0300 | [diff] [blame] | 70 | .is_super_speed = false, |
| 71 | .is_high_speed = true, |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 72 | }; |
| 73 | module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO); |
| 74 | MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection"); |
Tatyana Brokhman | 7eca4c5 | 2011-06-29 16:41:53 +0300 | [diff] [blame] | 75 | module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO); |
| 76 | MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /*-------------------------------------------------------------------------*/ |
| 78 | |
| 79 | /* gadget side driver data structres */ |
| 80 | struct dummy_ep { |
| 81 | struct list_head queue; |
| 82 | unsigned long last_io; /* jiffies timestamp */ |
| 83 | struct usb_gadget *gadget; |
| 84 | const struct usb_endpoint_descriptor *desc; |
| 85 | struct usb_ep ep; |
| 86 | unsigned halted : 1; |
Alan Stern | 851a526 | 2008-08-14 15:48:30 -0400 | [diff] [blame] | 87 | unsigned wedged : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | unsigned already_seen : 1; |
| 89 | unsigned setup_stage : 1; |
| 90 | }; |
| 91 | |
| 92 | struct dummy_request { |
| 93 | struct list_head queue; /* ep's requests */ |
| 94 | struct usb_request req; |
| 95 | }; |
| 96 | |
| 97 | static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep) |
| 98 | { |
| 99 | return container_of (_ep, struct dummy_ep, ep); |
| 100 | } |
| 101 | |
| 102 | static inline struct dummy_request *usb_request_to_dummy_request |
| 103 | (struct usb_request *_req) |
| 104 | { |
| 105 | return container_of (_req, struct dummy_request, req); |
| 106 | } |
| 107 | |
| 108 | /*-------------------------------------------------------------------------*/ |
| 109 | |
| 110 | /* |
| 111 | * Every device has ep0 for control requests, plus up to 30 more endpoints, |
| 112 | * in one of two types: |
| 113 | * |
| 114 | * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint |
| 115 | * number can be changed. Names like "ep-a" are used for this type. |
| 116 | * |
| 117 | * - Fixed Function: in other cases. some characteristics may be mutable; |
| 118 | * that'd be hardware-specific. Names like "ep12out-bulk" are used. |
| 119 | * |
| 120 | * Gadget drivers are responsible for not setting up conflicting endpoint |
| 121 | * configurations, illegal or unsupported packet lengths, and so on. |
| 122 | */ |
| 123 | |
| 124 | static const char ep0name [] = "ep0"; |
| 125 | |
| 126 | static const char *const ep_name [] = { |
| 127 | ep0name, /* everyone has ep0 */ |
| 128 | |
| 129 | /* act like a net2280: high speed, six configurable endpoints */ |
| 130 | "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f", |
| 131 | |
| 132 | /* or like pxa250: fifteen fixed function endpoints */ |
| 133 | "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int", |
| 134 | "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int", |
| 135 | "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso", |
| 136 | "ep15in-int", |
| 137 | |
| 138 | /* or like sa1100: two fixed function endpoints */ |
| 139 | "ep1out-bulk", "ep2in-bulk", |
| 140 | }; |
Tobias Klauser | 52950ed | 2005-12-11 16:20:08 +0100 | [diff] [blame] | 141 | #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 143 | /*-------------------------------------------------------------------------*/ |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | #define FIFO_SIZE 64 |
| 146 | |
| 147 | struct urbp { |
| 148 | struct urb *urb; |
| 149 | struct list_head urbp_list; |
| 150 | }; |
| 151 | |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 152 | |
| 153 | enum dummy_rh_state { |
| 154 | DUMMY_RH_RESET, |
| 155 | DUMMY_RH_SUSPENDED, |
| 156 | DUMMY_RH_RUNNING |
| 157 | }; |
| 158 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 159 | struct dummy_hcd { |
| 160 | struct dummy *dum; |
| 161 | enum dummy_rh_state rh_state; |
| 162 | struct timer_list timer; |
| 163 | u32 port_status; |
| 164 | u32 old_status; |
| 165 | unsigned long re_timeout; |
| 166 | |
| 167 | struct usb_device *udev; |
| 168 | struct list_head urbp_list; |
| 169 | |
| 170 | unsigned active:1; |
| 171 | unsigned old_active:1; |
| 172 | unsigned resuming:1; |
| 173 | }; |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | struct dummy { |
| 176 | spinlock_t lock; |
| 177 | |
| 178 | /* |
| 179 | * SLAVE/GADGET side support |
| 180 | */ |
| 181 | struct dummy_ep ep [DUMMY_ENDPOINTS]; |
| 182 | int address; |
| 183 | struct usb_gadget gadget; |
| 184 | struct usb_gadget_driver *driver; |
| 185 | struct dummy_request fifo_req; |
| 186 | u8 fifo_buf [FIFO_SIZE]; |
| 187 | u16 devstatus; |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 188 | unsigned udc_suspended:1; |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 189 | unsigned pullup:1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * MASTER/HOST side support |
| 193 | */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 194 | struct dummy_hcd *hs_hcd; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 195 | struct dummy_hcd *ss_hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 198 | static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 200 | return (struct dummy_hcd *) (hcd->hcd_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 203 | static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
| 205 | return container_of((void *) dum, struct usb_hcd, hcd_priv); |
| 206 | } |
| 207 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 208 | static inline struct device *dummy_dev(struct dummy_hcd *dum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 210 | return dummy_hcd_to_hcd(dum)->self.controller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 213 | static inline struct device *udc_dev (struct dummy *dum) |
| 214 | { |
| 215 | return dum->gadget.dev.parent; |
| 216 | } |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | static inline struct dummy *ep_to_dummy (struct dummy_ep *ep) |
| 219 | { |
| 220 | return container_of (ep->gadget, struct dummy, gadget); |
| 221 | } |
| 222 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 223 | static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 225 | struct dummy *dum = container_of(gadget, struct dummy, gadget); |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 226 | if (dum->gadget.speed == USB_SPEED_SUPER) |
| 227 | return dum->ss_hcd; |
| 228 | else |
| 229 | return dum->hs_hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static inline struct dummy *gadget_dev_to_dummy (struct device *dev) |
| 233 | { |
| 234 | return container_of (dev, struct dummy, gadget.dev); |
| 235 | } |
| 236 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 237 | static struct dummy the_controller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | /*-------------------------------------------------------------------------*/ |
| 240 | |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 241 | /* SLAVE/GADGET SIDE UTILITY ROUTINES */ |
| 242 | |
| 243 | /* called with spinlock held */ |
| 244 | static void nuke (struct dummy *dum, struct dummy_ep *ep) |
| 245 | { |
| 246 | while (!list_empty (&ep->queue)) { |
| 247 | struct dummy_request *req; |
| 248 | |
| 249 | req = list_entry (ep->queue.next, struct dummy_request, queue); |
| 250 | list_del_init (&req->queue); |
| 251 | req->req.status = -ESHUTDOWN; |
| 252 | |
| 253 | spin_unlock (&dum->lock); |
| 254 | req->req.complete (&ep->ep, &req->req); |
| 255 | spin_lock (&dum->lock); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* caller must hold lock */ |
| 260 | static void |
| 261 | stop_activity (struct dummy *dum) |
| 262 | { |
| 263 | struct dummy_ep *ep; |
| 264 | |
| 265 | /* prevent any more requests */ |
| 266 | dum->address = 0; |
| 267 | |
| 268 | /* The timer is left running so that outstanding URBs can fail */ |
| 269 | |
| 270 | /* nuke any pending requests first, so driver i/o is quiesced */ |
| 271 | list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list) |
| 272 | nuke (dum, ep); |
| 273 | |
| 274 | /* driver now does any non-usb quiescing necessary */ |
| 275 | } |
| 276 | |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 277 | /** |
| 278 | * set_link_state_by_speed() - Sets the current state of the link according to |
| 279 | * the hcd speed |
| 280 | * @dum_hcd: pointer to the dummy_hcd structure to update the link state for |
| 281 | * |
| 282 | * This function updates the port_status according to the link state and the |
| 283 | * speed of the hcd. |
| 284 | */ |
| 285 | static void set_link_state_by_speed(struct dummy_hcd *dum_hcd) |
| 286 | { |
| 287 | struct dummy *dum = dum_hcd->dum; |
| 288 | |
| 289 | if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) { |
| 290 | if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) { |
| 291 | dum_hcd->port_status = 0; |
| 292 | } else if (!dum->pullup || dum->udc_suspended) { |
| 293 | /* UDC suspend must cause a disconnect */ |
| 294 | dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION | |
| 295 | USB_PORT_STAT_ENABLE); |
| 296 | if ((dum_hcd->old_status & |
| 297 | USB_PORT_STAT_CONNECTION) != 0) |
| 298 | dum_hcd->port_status |= |
| 299 | (USB_PORT_STAT_C_CONNECTION << 16); |
| 300 | } else { |
| 301 | /* device is connected and not suspended */ |
| 302 | dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION | |
| 303 | USB_PORT_STAT_SPEED_5GBPS) ; |
| 304 | if ((dum_hcd->old_status & |
| 305 | USB_PORT_STAT_CONNECTION) == 0) |
| 306 | dum_hcd->port_status |= |
| 307 | (USB_PORT_STAT_C_CONNECTION << 16); |
| 308 | if ((dum_hcd->port_status & |
| 309 | USB_PORT_STAT_ENABLE) == 1 && |
| 310 | (dum_hcd->port_status & |
| 311 | USB_SS_PORT_LS_U0) == 1 && |
| 312 | dum_hcd->rh_state != DUMMY_RH_SUSPENDED) |
| 313 | dum_hcd->active = 1; |
| 314 | } |
| 315 | } else { |
| 316 | if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) { |
| 317 | dum_hcd->port_status = 0; |
| 318 | } else if (!dum->pullup || dum->udc_suspended) { |
| 319 | /* UDC suspend must cause a disconnect */ |
| 320 | dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION | |
| 321 | USB_PORT_STAT_ENABLE | |
| 322 | USB_PORT_STAT_LOW_SPEED | |
| 323 | USB_PORT_STAT_HIGH_SPEED | |
| 324 | USB_PORT_STAT_SUSPEND); |
| 325 | if ((dum_hcd->old_status & |
| 326 | USB_PORT_STAT_CONNECTION) != 0) |
| 327 | dum_hcd->port_status |= |
| 328 | (USB_PORT_STAT_C_CONNECTION << 16); |
| 329 | } else { |
| 330 | dum_hcd->port_status |= USB_PORT_STAT_CONNECTION; |
| 331 | if ((dum_hcd->old_status & |
| 332 | USB_PORT_STAT_CONNECTION) == 0) |
| 333 | dum_hcd->port_status |= |
| 334 | (USB_PORT_STAT_C_CONNECTION << 16); |
| 335 | if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0) |
| 336 | dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND; |
| 337 | else if ((dum_hcd->port_status & |
| 338 | USB_PORT_STAT_SUSPEND) == 0 && |
| 339 | dum_hcd->rh_state != DUMMY_RH_SUSPENDED) |
| 340 | dum_hcd->active = 1; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 345 | /* caller must hold lock */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 346 | static void set_link_state(struct dummy_hcd *dum_hcd) |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 347 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 348 | struct dummy *dum = dum_hcd->dum; |
| 349 | |
| 350 | dum_hcd->active = 0; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 351 | if (dum->pullup) |
| 352 | if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 && |
| 353 | dum->gadget.speed != USB_SPEED_SUPER) || |
| 354 | (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 && |
| 355 | dum->gadget.speed == USB_SPEED_SUPER)) |
| 356 | return; |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 357 | |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 358 | set_link_state_by_speed(dum_hcd); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 359 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 360 | if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 || |
| 361 | dum_hcd->active) |
| 362 | dum_hcd->resuming = 0; |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 363 | |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 364 | /* if !connected or reset */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 365 | if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 || |
| 366 | (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) { |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 367 | /* |
| 368 | * We're connected and not reset (reset occurred now), |
| 369 | * and driver attached - disconnect! |
| 370 | */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 371 | if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 && |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 372 | (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 && |
| 373 | dum->driver) { |
| 374 | stop_activity(dum); |
| 375 | spin_unlock(&dum->lock); |
| 376 | dum->driver->disconnect(&dum->gadget); |
| 377 | spin_lock(&dum->lock); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 378 | } |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 379 | } else if (dum_hcd->active != dum_hcd->old_active) { |
| 380 | if (dum_hcd->old_active && dum->driver->suspend) { |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 381 | spin_unlock(&dum->lock); |
| 382 | dum->driver->suspend(&dum->gadget); |
| 383 | spin_lock(&dum->lock); |
| 384 | } else if (!dum_hcd->old_active && dum->driver->resume) { |
| 385 | spin_unlock(&dum->lock); |
| 386 | dum->driver->resume(&dum->gadget); |
| 387 | spin_lock(&dum->lock); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 391 | dum_hcd->old_status = dum_hcd->port_status; |
| 392 | dum_hcd->old_active = dum_hcd->active; |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /*-------------------------------------------------------------------------*/ |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | /* SLAVE/GADGET SIDE DRIVER |
| 398 | * |
| 399 | * This only tracks gadget state. All the work is done when the host |
| 400 | * side tries some (emulated) i/o operation. Real device controller |
| 401 | * drivers would do real i/o using dma, fifos, irqs, timers, etc. |
| 402 | */ |
| 403 | |
| 404 | #define is_enabled(dum) \ |
| 405 | (dum->port_status & USB_PORT_STAT_ENABLE) |
| 406 | |
| 407 | static int |
| 408 | dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc) |
| 409 | { |
| 410 | struct dummy *dum; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 411 | struct dummy_hcd *dum_hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | struct dummy_ep *ep; |
| 413 | unsigned max; |
| 414 | int retval; |
| 415 | |
| 416 | ep = usb_ep_to_dummy_ep (_ep); |
| 417 | if (!_ep || !desc || ep->desc || _ep->name == ep0name |
| 418 | || desc->bDescriptorType != USB_DT_ENDPOINT) |
| 419 | return -EINVAL; |
| 420 | dum = ep_to_dummy (ep); |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 421 | if (!dum->driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | return -ESHUTDOWN; |
Sebastian Andrzej Siewior | 719e52c | 2011-06-16 20:36:57 +0200 | [diff] [blame] | 423 | |
| 424 | dum_hcd = gadget_to_dummy_hcd(&dum->gadget); |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 425 | if (!is_enabled(dum_hcd)) |
| 426 | return -ESHUTDOWN; |
| 427 | |
| 428 | /* |
| 429 | * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the |
| 430 | * maximum packet size. |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 431 | * For SS devices the wMaxPacketSize is limited by 1024. |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 432 | */ |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 433 | max = usb_endpoint_maxp(desc) & 0x7ff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | /* drivers must not request bad settings, since lower levels |
| 436 | * (hardware or its drivers) may not check. some endpoints |
| 437 | * can't do iso, many have maxpacket limitations, etc. |
| 438 | * |
| 439 | * since this "hardware" driver is here to help debugging, we |
| 440 | * have some extra sanity checks. (there could be more though, |
| 441 | * especially for "ep9out" style fixed function ones.) |
| 442 | */ |
| 443 | retval = -EINVAL; |
| 444 | switch (desc->bmAttributes & 0x03) { |
| 445 | case USB_ENDPOINT_XFER_BULK: |
| 446 | if (strstr (ep->ep.name, "-iso") |
| 447 | || strstr (ep->ep.name, "-int")) { |
| 448 | goto done; |
| 449 | } |
| 450 | switch (dum->gadget.speed) { |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 451 | case USB_SPEED_SUPER: |
| 452 | if (max == 1024) |
| 453 | break; |
| 454 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | case USB_SPEED_HIGH: |
| 456 | if (max == 512) |
| 457 | break; |
Ingo van Lil | 9063ff4 | 2008-03-28 14:50:26 -0700 | [diff] [blame] | 458 | goto done; |
| 459 | case USB_SPEED_FULL: |
| 460 | if (max == 8 || max == 16 || max == 32 || max == 64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | /* we'll fake any legal size */ |
| 462 | break; |
Ingo van Lil | 9063ff4 | 2008-03-28 14:50:26 -0700 | [diff] [blame] | 463 | /* save a return statement */ |
| 464 | default: |
| 465 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | break; |
| 468 | case USB_ENDPOINT_XFER_INT: |
| 469 | if (strstr (ep->ep.name, "-iso")) /* bulk is ok */ |
| 470 | goto done; |
| 471 | /* real hardware might not handle all packet sizes */ |
| 472 | switch (dum->gadget.speed) { |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 473 | case USB_SPEED_SUPER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | case USB_SPEED_HIGH: |
| 475 | if (max <= 1024) |
| 476 | break; |
| 477 | /* save a return statement */ |
| 478 | case USB_SPEED_FULL: |
| 479 | if (max <= 64) |
| 480 | break; |
| 481 | /* save a return statement */ |
| 482 | default: |
| 483 | if (max <= 8) |
| 484 | break; |
| 485 | goto done; |
| 486 | } |
| 487 | break; |
| 488 | case USB_ENDPOINT_XFER_ISOC: |
| 489 | if (strstr (ep->ep.name, "-bulk") |
| 490 | || strstr (ep->ep.name, "-int")) |
| 491 | goto done; |
| 492 | /* real hardware might not handle all packet sizes */ |
| 493 | switch (dum->gadget.speed) { |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 494 | case USB_SPEED_SUPER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | case USB_SPEED_HIGH: |
| 496 | if (max <= 1024) |
| 497 | break; |
| 498 | /* save a return statement */ |
| 499 | case USB_SPEED_FULL: |
| 500 | if (max <= 1023) |
| 501 | break; |
| 502 | /* save a return statement */ |
| 503 | default: |
| 504 | goto done; |
| 505 | } |
| 506 | break; |
| 507 | default: |
| 508 | /* few chips support control except on ep0 */ |
| 509 | goto done; |
| 510 | } |
| 511 | |
| 512 | _ep->maxpacket = max; |
| 513 | ep->desc = desc; |
| 514 | |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 515 | dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | _ep->name, |
| 517 | desc->bEndpointAddress & 0x0f, |
| 518 | (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out", |
| 519 | ({ char *val; |
| 520 | switch (desc->bmAttributes & 0x03) { |
Tatyana Brokhman | 7c884fe | 2011-06-28 16:33:52 +0300 | [diff] [blame] | 521 | case USB_ENDPOINT_XFER_BULK: |
| 522 | val = "bulk"; |
| 523 | break; |
| 524 | case USB_ENDPOINT_XFER_ISOC: |
| 525 | val = "iso"; |
| 526 | break; |
| 527 | case USB_ENDPOINT_XFER_INT: |
| 528 | val = "intr"; |
| 529 | break; |
| 530 | default: |
| 531 | val = "ctrl"; |
| 532 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | }; val; }), |
| 534 | max); |
| 535 | |
| 536 | /* at this point real hardware should be NAKing transfers |
| 537 | * to that endpoint, until a buffer is queued to it. |
| 538 | */ |
Alan Stern | 851a526 | 2008-08-14 15:48:30 -0400 | [diff] [blame] | 539 | ep->halted = ep->wedged = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | retval = 0; |
| 541 | done: |
| 542 | return retval; |
| 543 | } |
| 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | static int dummy_disable (struct usb_ep *_ep) |
| 546 | { |
| 547 | struct dummy_ep *ep; |
| 548 | struct dummy *dum; |
| 549 | unsigned long flags; |
| 550 | int retval; |
| 551 | |
| 552 | ep = usb_ep_to_dummy_ep (_ep); |
| 553 | if (!_ep || !ep->desc || _ep->name == ep0name) |
| 554 | return -EINVAL; |
| 555 | dum = ep_to_dummy (ep); |
| 556 | |
| 557 | spin_lock_irqsave (&dum->lock, flags); |
| 558 | ep->desc = NULL; |
| 559 | retval = 0; |
| 560 | nuke (dum, ep); |
| 561 | spin_unlock_irqrestore (&dum->lock, flags); |
| 562 | |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 563 | dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | return retval; |
| 565 | } |
| 566 | |
| 567 | static struct usb_request * |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 568 | dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
| 570 | struct dummy_ep *ep; |
| 571 | struct dummy_request *req; |
| 572 | |
| 573 | if (!_ep) |
| 574 | return NULL; |
| 575 | ep = usb_ep_to_dummy_ep (_ep); |
| 576 | |
Eric Sesterhenn | 7039f42 | 2006-02-27 13:34:10 -0800 | [diff] [blame] | 577 | req = kzalloc(sizeof(*req), mem_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | if (!req) |
| 579 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | INIT_LIST_HEAD (&req->queue); |
| 581 | return &req->req; |
| 582 | } |
| 583 | |
| 584 | static void |
| 585 | dummy_free_request (struct usb_ep *_ep, struct usb_request *_req) |
| 586 | { |
| 587 | struct dummy_ep *ep; |
| 588 | struct dummy_request *req; |
| 589 | |
| 590 | ep = usb_ep_to_dummy_ep (_ep); |
| 591 | if (!ep || !_req || (!ep->desc && _ep->name != ep0name)) |
| 592 | return; |
| 593 | |
| 594 | req = usb_request_to_dummy_request (_req); |
| 595 | WARN_ON (!list_empty (&req->queue)); |
| 596 | kfree (req); |
| 597 | } |
| 598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | static void |
| 600 | fifo_complete (struct usb_ep *ep, struct usb_request *req) |
| 601 | { |
| 602 | } |
| 603 | |
| 604 | static int |
Olav Kongas | 5db539e | 2005-06-23 20:25:36 +0300 | [diff] [blame] | 605 | dummy_queue (struct usb_ep *_ep, struct usb_request *_req, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 606 | gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
| 608 | struct dummy_ep *ep; |
| 609 | struct dummy_request *req; |
| 610 | struct dummy *dum; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 611 | struct dummy_hcd *dum_hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | unsigned long flags; |
| 613 | |
| 614 | req = usb_request_to_dummy_request (_req); |
| 615 | if (!_req || !list_empty (&req->queue) || !_req->complete) |
| 616 | return -EINVAL; |
| 617 | |
| 618 | ep = usb_ep_to_dummy_ep (_ep); |
| 619 | if (!_ep || (!ep->desc && _ep->name != ep0name)) |
| 620 | return -EINVAL; |
| 621 | |
| 622 | dum = ep_to_dummy (ep); |
Sebastian Andrzej Siewior | 719e52c | 2011-06-16 20:36:57 +0200 | [diff] [blame] | 623 | dum_hcd = gadget_to_dummy_hcd(&dum->gadget); |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 624 | if (!dum->driver || !is_enabled(dum_hcd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | return -ESHUTDOWN; |
| 626 | |
| 627 | #if 0 |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 628 | dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | ep, _req, _ep->name, _req->length, _req->buf); |
| 630 | #endif |
| 631 | |
| 632 | _req->status = -EINPROGRESS; |
| 633 | _req->actual = 0; |
| 634 | spin_lock_irqsave (&dum->lock, flags); |
| 635 | |
| 636 | /* implement an emulated single-request FIFO */ |
| 637 | if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) && |
| 638 | list_empty (&dum->fifo_req.queue) && |
| 639 | list_empty (&ep->queue) && |
| 640 | _req->length <= FIFO_SIZE) { |
| 641 | req = &dum->fifo_req; |
| 642 | req->req = *_req; |
| 643 | req->req.buf = dum->fifo_buf; |
| 644 | memcpy (dum->fifo_buf, _req->buf, _req->length); |
| 645 | req->req.context = dum; |
| 646 | req->req.complete = fifo_complete; |
| 647 | |
David Brownell | c728df7 | 2008-07-26 08:06:24 -0700 | [diff] [blame] | 648 | list_add_tail(&req->queue, &ep->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | spin_unlock (&dum->lock); |
| 650 | _req->actual = _req->length; |
| 651 | _req->status = 0; |
| 652 | _req->complete (_ep, _req); |
| 653 | spin_lock (&dum->lock); |
David Brownell | c728df7 | 2008-07-26 08:06:24 -0700 | [diff] [blame] | 654 | } else |
| 655 | list_add_tail(&req->queue, &ep->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | spin_unlock_irqrestore (&dum->lock, flags); |
| 657 | |
| 658 | /* real hardware would likely enable transfers here, in case |
| 659 | * it'd been left NAKing. |
| 660 | */ |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req) |
| 665 | { |
| 666 | struct dummy_ep *ep; |
| 667 | struct dummy *dum; |
| 668 | int retval = -EINVAL; |
| 669 | unsigned long flags; |
| 670 | struct dummy_request *req = NULL; |
| 671 | |
| 672 | if (!_ep || !_req) |
| 673 | return retval; |
| 674 | ep = usb_ep_to_dummy_ep (_ep); |
| 675 | dum = ep_to_dummy (ep); |
| 676 | |
| 677 | if (!dum->driver) |
| 678 | return -ESHUTDOWN; |
| 679 | |
Alan Stern | b4dbda1 | 2006-07-28 17:07:34 -0400 | [diff] [blame] | 680 | local_irq_save (flags); |
| 681 | spin_lock (&dum->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | list_for_each_entry (req, &ep->queue, queue) { |
| 683 | if (&req->req == _req) { |
| 684 | list_del_init (&req->queue); |
| 685 | _req->status = -ECONNRESET; |
| 686 | retval = 0; |
| 687 | break; |
| 688 | } |
| 689 | } |
Alan Stern | b4dbda1 | 2006-07-28 17:07:34 -0400 | [diff] [blame] | 690 | spin_unlock (&dum->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | if (retval == 0) { |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 693 | dev_dbg (udc_dev(dum), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | "dequeued req %p from %s, len %d buf %p\n", |
| 695 | req, _ep->name, _req->length, _req->buf); |
| 696 | _req->complete (_ep, _req); |
| 697 | } |
Alan Stern | b4dbda1 | 2006-07-28 17:07:34 -0400 | [diff] [blame] | 698 | local_irq_restore (flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | return retval; |
| 700 | } |
| 701 | |
| 702 | static int |
Alan Stern | 851a526 | 2008-08-14 15:48:30 -0400 | [diff] [blame] | 703 | dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | { |
| 705 | struct dummy_ep *ep; |
| 706 | struct dummy *dum; |
| 707 | |
| 708 | if (!_ep) |
| 709 | return -EINVAL; |
| 710 | ep = usb_ep_to_dummy_ep (_ep); |
| 711 | dum = ep_to_dummy (ep); |
| 712 | if (!dum->driver) |
| 713 | return -ESHUTDOWN; |
| 714 | if (!value) |
Alan Stern | 851a526 | 2008-08-14 15:48:30 -0400 | [diff] [blame] | 715 | ep->halted = ep->wedged = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) && |
| 717 | !list_empty (&ep->queue)) |
| 718 | return -EAGAIN; |
Alan Stern | 851a526 | 2008-08-14 15:48:30 -0400 | [diff] [blame] | 719 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | ep->halted = 1; |
Alan Stern | 851a526 | 2008-08-14 15:48:30 -0400 | [diff] [blame] | 721 | if (wedged) |
| 722 | ep->wedged = 1; |
| 723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | /* FIXME clear emulated data toggle too */ |
| 725 | return 0; |
| 726 | } |
| 727 | |
Alan Stern | 851a526 | 2008-08-14 15:48:30 -0400 | [diff] [blame] | 728 | static int |
| 729 | dummy_set_halt(struct usb_ep *_ep, int value) |
| 730 | { |
| 731 | return dummy_set_halt_and_wedge(_ep, value, 0); |
| 732 | } |
| 733 | |
| 734 | static int dummy_set_wedge(struct usb_ep *_ep) |
| 735 | { |
| 736 | if (!_ep || _ep->name == ep0name) |
| 737 | return -EINVAL; |
| 738 | return dummy_set_halt_and_wedge(_ep, 1, 1); |
| 739 | } |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | static const struct usb_ep_ops dummy_ep_ops = { |
| 742 | .enable = dummy_enable, |
| 743 | .disable = dummy_disable, |
| 744 | |
| 745 | .alloc_request = dummy_alloc_request, |
| 746 | .free_request = dummy_free_request, |
| 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | .queue = dummy_queue, |
| 749 | .dequeue = dummy_dequeue, |
| 750 | |
| 751 | .set_halt = dummy_set_halt, |
Alan Stern | 851a526 | 2008-08-14 15:48:30 -0400 | [diff] [blame] | 752 | .set_wedge = dummy_set_wedge, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | }; |
| 754 | |
| 755 | /*-------------------------------------------------------------------------*/ |
| 756 | |
| 757 | /* there are both host and device side versions of this call ... */ |
| 758 | static int dummy_g_get_frame (struct usb_gadget *_gadget) |
| 759 | { |
| 760 | struct timeval tv; |
| 761 | |
| 762 | do_gettimeofday (&tv); |
| 763 | return tv.tv_usec / 1000; |
| 764 | } |
| 765 | |
| 766 | static int dummy_wakeup (struct usb_gadget *_gadget) |
| 767 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 768 | struct dummy_hcd *dum_hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 770 | dum_hcd = gadget_to_dummy_hcd(_gadget); |
| 771 | if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE) |
Alan Stern | 5742b0c | 2005-05-02 11:25:17 -0400 | [diff] [blame] | 772 | | (1 << USB_DEVICE_REMOTE_WAKEUP)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | return -EINVAL; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 774 | if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 775 | return -ENOLINK; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 776 | if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 && |
| 777 | dum_hcd->rh_state != DUMMY_RH_SUSPENDED) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 778 | return -EIO; |
| 779 | |
| 780 | /* FIXME: What if the root hub is suspended but the port isn't? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | /* hub notices our request, issues downstream resume, etc */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 783 | dum_hcd->resuming = 1; |
| 784 | dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20); |
| 785 | mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value) |
| 790 | { |
| 791 | struct dummy *dum; |
| 792 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 793 | dum = (gadget_to_dummy_hcd(_gadget))->dum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | if (value) |
| 795 | dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED); |
| 796 | else |
| 797 | dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED); |
| 798 | return 0; |
| 799 | } |
| 800 | |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 801 | static void dummy_udc_udpate_ep0(struct dummy *dum) |
| 802 | { |
Sebastian Andrzej Siewior | c688419 | 2012-01-09 13:14:56 +0100 | [diff] [blame^] | 803 | if (dum->gadget.speed == USB_SPEED_SUPER) |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 804 | dum->ep[0].ep.maxpacket = 9; |
Sebastian Andrzej Siewior | c688419 | 2012-01-09 13:14:56 +0100 | [diff] [blame^] | 805 | else |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 806 | dum->ep[0].ep.maxpacket = 64; |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 809 | static int dummy_pullup (struct usb_gadget *_gadget, int value) |
| 810 | { |
Sebastian Andrzej Siewior | d8a14a8 | 2011-06-17 10:11:41 +0200 | [diff] [blame] | 811 | struct dummy_hcd *dum_hcd; |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 812 | struct dummy *dum; |
| 813 | unsigned long flags; |
| 814 | |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 815 | dum = gadget_dev_to_dummy(&_gadget->dev); |
| 816 | |
| 817 | if (value && dum->driver) { |
| 818 | if (mod_data.is_super_speed) |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 819 | dum->gadget.speed = dum->driver->max_speed; |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 820 | else if (mod_data.is_high_speed) |
| 821 | dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 822 | dum->driver->max_speed); |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 823 | else |
| 824 | dum->gadget.speed = USB_SPEED_FULL; |
| 825 | dummy_udc_udpate_ep0(dum); |
| 826 | |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 827 | if (dum->gadget.speed < dum->driver->max_speed) |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 828 | dev_dbg(udc_dev(dum), "This device can perform faster" |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 829 | " if you connect it to a %s port...\n", |
| 830 | usb_speed_string(dum->driver->max_speed)); |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 831 | } |
Sebastian Andrzej Siewior | d8a14a8 | 2011-06-17 10:11:41 +0200 | [diff] [blame] | 832 | dum_hcd = gadget_to_dummy_hcd(_gadget); |
Sebastian Andrzej Siewior | d8a14a8 | 2011-06-17 10:11:41 +0200 | [diff] [blame] | 833 | |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 834 | spin_lock_irqsave (&dum->lock, flags); |
| 835 | dum->pullup = (value != 0); |
Sebastian Andrzej Siewior | d8a14a8 | 2011-06-17 10:11:41 +0200 | [diff] [blame] | 836 | set_link_state(dum_hcd); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 837 | spin_unlock_irqrestore (&dum->lock, flags); |
Sebastian Andrzej Siewior | b573841 | 2011-06-23 14:26:14 +0200 | [diff] [blame] | 838 | |
Sebastian Andrzej Siewior | d8a14a8 | 2011-06-17 10:11:41 +0200 | [diff] [blame] | 839 | usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | |
Sebastian Andrzej Siewior | aa07473 | 2011-06-23 14:26:17 +0200 | [diff] [blame] | 843 | static int dummy_udc_start(struct usb_gadget *g, |
| 844 | struct usb_gadget_driver *driver); |
| 845 | static int dummy_udc_stop(struct usb_gadget *g, |
| 846 | struct usb_gadget_driver *driver); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | static const struct usb_gadget_ops dummy_ops = { |
| 849 | .get_frame = dummy_g_get_frame, |
| 850 | .wakeup = dummy_wakeup, |
| 851 | .set_selfpowered = dummy_set_selfpowered, |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 852 | .pullup = dummy_pullup, |
Sebastian Andrzej Siewior | aa07473 | 2011-06-23 14:26:17 +0200 | [diff] [blame] | 853 | .udc_start = dummy_udc_start, |
| 854 | .udc_stop = dummy_udc_stop, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | }; |
| 856 | |
| 857 | /*-------------------------------------------------------------------------*/ |
| 858 | |
| 859 | /* "function" sysfs attribute */ |
| 860 | static ssize_t |
Yani Ioannou | 10523b3 | 2005-05-17 06:43:37 -0400 | [diff] [blame] | 861 | show_function (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | { |
| 863 | struct dummy *dum = gadget_dev_to_dummy (dev); |
| 864 | |
| 865 | if (!dum->driver || !dum->driver->function) |
| 866 | return 0; |
| 867 | return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function); |
| 868 | } |
Alan Stern | cc095b0 | 2005-05-10 15:28:38 -0400 | [diff] [blame] | 869 | static DEVICE_ATTR (function, S_IRUGO, show_function, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
| 871 | /*-------------------------------------------------------------------------*/ |
| 872 | |
| 873 | /* |
| 874 | * Driver registration/unregistration. |
| 875 | * |
| 876 | * This is basically hardware-specific; there's usually only one real USB |
| 877 | * device (not host) controller since that's how USB devices are intended |
| 878 | * to work. So most implementations of these api calls will rely on the |
| 879 | * fact that only one driver will ever bind to the hardware. But curious |
| 880 | * hardware can be built with discrete components, so the gadget API doesn't |
| 881 | * require that assumption. |
| 882 | * |
| 883 | * For this emulator, it might be convenient to create a usb slave device |
| 884 | * for each driver that registers: just add to a big root hub. |
| 885 | */ |
| 886 | |
Sebastian Andrzej Siewior | aa07473 | 2011-06-23 14:26:17 +0200 | [diff] [blame] | 887 | static int dummy_udc_start(struct usb_gadget *g, |
| 888 | struct usb_gadget_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | { |
Sebastian Andrzej Siewior | aa07473 | 2011-06-23 14:26:17 +0200 | [diff] [blame] | 890 | struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g); |
| 891 | struct dummy *dum = dum_hcd->dum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 893 | if (driver->max_speed == USB_SPEED_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | return -EINVAL; |
| 895 | |
| 896 | /* |
| 897 | * SLAVE side init ... the layer above hardware, which |
| 898 | * can't enumerate without help from the driver we're binding. |
| 899 | */ |
Alan Stern | 5742b0c | 2005-05-02 11:25:17 -0400 | [diff] [blame] | 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | dum->devstatus = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | dum->driver = driver; |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 904 | dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | return 0; |
| 907 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Sebastian Andrzej Siewior | aa07473 | 2011-06-23 14:26:17 +0200 | [diff] [blame] | 909 | static int dummy_udc_stop(struct usb_gadget *g, |
| 910 | struct usb_gadget_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | { |
Sebastian Andrzej Siewior | aa07473 | 2011-06-23 14:26:17 +0200 | [diff] [blame] | 912 | struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g); |
| 913 | struct dummy *dum = dum_hcd->dum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 915 | dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | driver->driver.name); |
| 917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | dum->driver = NULL; |
Sebastian Andrzej Siewior | 2542787 | 2011-06-16 20:36:55 +0200 | [diff] [blame] | 919 | |
| 920 | dummy_pullup(&dum->gadget, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return 0; |
| 922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | #undef is_enabled |
| 925 | |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 926 | /* The gadget structure is stored inside the hcd structure and will be |
| 927 | * released along with it. */ |
| 928 | static void |
| 929 | dummy_gadget_release (struct device *dev) |
| 930 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 931 | return; |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 932 | } |
| 933 | |
Sebastian Andrzej Siewior | 0fb5759 | 2011-06-23 14:26:12 +0200 | [diff] [blame] | 934 | static void init_dummy_udc_hw(struct dummy *dum) |
| 935 | { |
| 936 | int i; |
| 937 | |
| 938 | INIT_LIST_HEAD(&dum->gadget.ep_list); |
| 939 | for (i = 0; i < DUMMY_ENDPOINTS; i++) { |
| 940 | struct dummy_ep *ep = &dum->ep[i]; |
| 941 | |
| 942 | if (!ep_name[i]) |
| 943 | break; |
| 944 | ep->ep.name = ep_name[i]; |
| 945 | ep->ep.ops = &dummy_ep_ops; |
| 946 | list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list); |
| 947 | ep->halted = ep->wedged = ep->already_seen = |
| 948 | ep->setup_stage = 0; |
| 949 | ep->ep.maxpacket = ~0; |
Sebastian Andrzej Siewior | c688419 | 2012-01-09 13:14:56 +0100 | [diff] [blame^] | 950 | ep->ep.max_streams = 16; |
Sebastian Andrzej Siewior | 0fb5759 | 2011-06-23 14:26:12 +0200 | [diff] [blame] | 951 | ep->last_io = jiffies; |
| 952 | ep->gadget = &dum->gadget; |
| 953 | ep->desc = NULL; |
| 954 | INIT_LIST_HEAD(&ep->queue); |
| 955 | } |
| 956 | |
| 957 | dum->gadget.ep0 = &dum->ep[0].ep; |
| 958 | list_del_init(&dum->ep[0].ep.ep_list); |
| 959 | INIT_LIST_HEAD(&dum->fifo_req.queue); |
Sebastian Andrzej Siewior | f8744d4 | 2011-06-23 14:26:13 +0200 | [diff] [blame] | 960 | |
| 961 | #ifdef CONFIG_USB_OTG |
| 962 | dum->gadget.is_otg = 1; |
| 963 | #endif |
Sebastian Andrzej Siewior | 0fb5759 | 2011-06-23 14:26:12 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Alan Stern | 8364d6b | 2005-11-14 12:16:30 -0500 | [diff] [blame] | 966 | static int dummy_udc_probe (struct platform_device *pdev) |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 967 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 968 | struct dummy *dum = &the_controller; |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 969 | int rc; |
| 970 | |
| 971 | dum->gadget.name = gadget_name; |
| 972 | dum->gadget.ops = &dummy_ops; |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 973 | dum->gadget.max_speed = USB_SPEED_SUPER; |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 974 | |
Kay Sievers | 0031a06 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 975 | dev_set_name(&dum->gadget.dev, "gadget"); |
Alan Stern | 8364d6b | 2005-11-14 12:16:30 -0500 | [diff] [blame] | 976 | dum->gadget.dev.parent = &pdev->dev; |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 977 | dum->gadget.dev.release = dummy_gadget_release; |
| 978 | rc = device_register (&dum->gadget.dev); |
Rahul Ruikar | 75d87cd | 2010-10-07 09:40:45 +0530 | [diff] [blame] | 979 | if (rc < 0) { |
| 980 | put_device(&dum->gadget.dev); |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 981 | return rc; |
Rahul Ruikar | 75d87cd | 2010-10-07 09:40:45 +0530 | [diff] [blame] | 982 | } |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 983 | |
Sebastian Andrzej Siewior | 0fb5759 | 2011-06-23 14:26:12 +0200 | [diff] [blame] | 984 | init_dummy_udc_hw(dum); |
| 985 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 986 | rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget); |
| 987 | if (rc < 0) |
| 988 | goto err_udc; |
| 989 | |
Alan Stern | efd54a3 | 2006-09-25 11:55:56 -0400 | [diff] [blame] | 990 | rc = device_create_file (&dum->gadget.dev, &dev_attr_function); |
| 991 | if (rc < 0) |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 992 | goto err_dev; |
| 993 | platform_set_drvdata(pdev, dum); |
| 994 | return rc; |
| 995 | |
| 996 | err_dev: |
| 997 | usb_del_gadget_udc(&dum->gadget); |
| 998 | err_udc: |
| 999 | device_unregister(&dum->gadget.dev); |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 1000 | return rc; |
| 1001 | } |
| 1002 | |
Alan Stern | 8364d6b | 2005-11-14 12:16:30 -0500 | [diff] [blame] | 1003 | static int dummy_udc_remove (struct platform_device *pdev) |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 1004 | { |
Alan Stern | 8364d6b | 2005-11-14 12:16:30 -0500 | [diff] [blame] | 1005 | struct dummy *dum = platform_get_drvdata (pdev); |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 1006 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1007 | usb_del_gadget_udc(&dum->gadget); |
Alan Stern | 8364d6b | 2005-11-14 12:16:30 -0500 | [diff] [blame] | 1008 | platform_set_drvdata (pdev, NULL); |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 1009 | device_remove_file (&dum->gadget.dev, &dev_attr_function); |
| 1010 | device_unregister (&dum->gadget.dev); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
Sebastian Andrzej Siewior | fc0b721 | 2011-06-17 19:43:13 +0200 | [diff] [blame] | 1014 | static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd, |
| 1015 | int suspend) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1016 | { |
Sebastian Andrzej Siewior | fc0b721 | 2011-06-17 19:43:13 +0200 | [diff] [blame] | 1017 | spin_lock_irq(&dum->lock); |
| 1018 | dum->udc_suspended = suspend; |
Sebastian Andrzej Siewior | d8a14a8 | 2011-06-17 10:11:41 +0200 | [diff] [blame] | 1019 | set_link_state(dum_hcd); |
Sebastian Andrzej Siewior | fc0b721 | 2011-06-17 19:43:13 +0200 | [diff] [blame] | 1020 | spin_unlock_irq(&dum->lock); |
| 1021 | } |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1022 | |
Sebastian Andrzej Siewior | fc0b721 | 2011-06-17 19:43:13 +0200 | [diff] [blame] | 1023 | static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state) |
| 1024 | { |
| 1025 | struct dummy *dum = platform_get_drvdata(pdev); |
| 1026 | struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget); |
| 1027 | |
| 1028 | dev_dbg(&pdev->dev, "%s\n", __func__); |
| 1029 | dummy_udc_pm(dum, dum_hcd, 1); |
Sebastian Andrzej Siewior | d8a14a8 | 2011-06-17 10:11:41 +0200 | [diff] [blame] | 1030 | usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1031 | return 0; |
| 1032 | } |
| 1033 | |
Sebastian Andrzej Siewior | fc0b721 | 2011-06-17 19:43:13 +0200 | [diff] [blame] | 1034 | static int dummy_udc_resume(struct platform_device *pdev) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1035 | { |
Sebastian Andrzej Siewior | fc0b721 | 2011-06-17 19:43:13 +0200 | [diff] [blame] | 1036 | struct dummy *dum = platform_get_drvdata(pdev); |
| 1037 | struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget); |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1038 | |
Sebastian Andrzej Siewior | fc0b721 | 2011-06-17 19:43:13 +0200 | [diff] [blame] | 1039 | dev_dbg(&pdev->dev, "%s\n", __func__); |
| 1040 | dummy_udc_pm(dum, dum_hcd, 0); |
Sebastian Andrzej Siewior | d8a14a8 | 2011-06-17 10:11:41 +0200 | [diff] [blame] | 1041 | usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1042 | return 0; |
| 1043 | } |
| 1044 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1045 | static struct platform_driver dummy_udc_driver = { |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 1046 | .probe = dummy_udc_probe, |
| 1047 | .remove = dummy_udc_remove, |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1048 | .suspend = dummy_udc_suspend, |
| 1049 | .resume = dummy_udc_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1050 | .driver = { |
| 1051 | .name = (char *) gadget_name, |
| 1052 | .owner = THIS_MODULE, |
| 1053 | }, |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 1054 | }; |
| 1055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | /*-------------------------------------------------------------------------*/ |
| 1057 | |
| 1058 | /* MASTER/HOST SIDE DRIVER |
| 1059 | * |
| 1060 | * this uses the hcd framework to hook up to host side drivers. |
| 1061 | * its root hub will only have one device, otherwise it acts like |
| 1062 | * a normal host controller. |
| 1063 | * |
| 1064 | * when urbs are queued, they're just stuck on a list that we |
| 1065 | * scan in a timer callback. that callback connects writes from |
| 1066 | * the host with reads from the device, and so on, based on the |
| 1067 | * usb 2.0 rules. |
| 1068 | */ |
| 1069 | |
| 1070 | static int dummy_urb_enqueue ( |
| 1071 | struct usb_hcd *hcd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | struct urb *urb, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1073 | gfp_t mem_flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | ) { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1075 | struct dummy_hcd *dum_hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | struct urbp *urbp; |
| 1077 | unsigned long flags; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1078 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
| 1080 | if (!urb->transfer_buffer && urb->transfer_buffer_length) |
| 1081 | return -EINVAL; |
| 1082 | |
| 1083 | urbp = kmalloc (sizeof *urbp, mem_flags); |
| 1084 | if (!urbp) |
| 1085 | return -ENOMEM; |
| 1086 | urbp->urb = urb; |
| 1087 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1088 | dum_hcd = hcd_to_dummy_hcd(hcd); |
| 1089 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1090 | rc = usb_hcd_link_urb_to_ep(hcd, urb); |
| 1091 | if (rc) { |
| 1092 | kfree(urbp); |
| 1093 | goto done; |
| 1094 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1096 | if (!dum_hcd->udev) { |
| 1097 | dum_hcd->udev = urb->dev; |
| 1098 | usb_get_dev(dum_hcd->udev); |
| 1099 | } else if (unlikely(dum_hcd->udev != urb->dev)) |
| 1100 | dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1102 | list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | urb->hcpriv = urbp; |
| 1104 | if (usb_pipetype (urb->pipe) == PIPE_CONTROL) |
| 1105 | urb->error_count = 1; /* mark as a new urb */ |
| 1106 | |
| 1107 | /* kick the scheduler, it'll do the rest */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1108 | if (!timer_pending(&dum_hcd->timer)) |
| 1109 | mod_timer(&dum_hcd->timer, jiffies + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1111 | done: |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1112 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1113 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1116 | static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1118 | struct dummy_hcd *dum_hcd; |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1119 | unsigned long flags; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1120 | int rc; |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1121 | |
| 1122 | /* giveback happens automatically in timer callback, |
| 1123 | * so make sure the callback happens */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1124 | dum_hcd = hcd_to_dummy_hcd(hcd); |
| 1125 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1126 | |
| 1127 | rc = usb_hcd_check_unlink_urb(hcd, urb, status); |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1128 | if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING && |
| 1129 | !list_empty(&dum_hcd->urbp_list)) |
| 1130 | mod_timer(&dum_hcd->timer, jiffies); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1131 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1132 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1133 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | /* transfer up to a frame's worth; caller must own lock */ |
| 1137 | static int |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1138 | transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit, |
| 1139 | int *status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | { |
| 1141 | struct dummy_request *req; |
| 1142 | |
| 1143 | top: |
| 1144 | /* if there's no request queued, the device is NAKing; return */ |
| 1145 | list_for_each_entry (req, &ep->queue, queue) { |
| 1146 | unsigned host_len, dev_len, len; |
| 1147 | int is_short, to_host; |
| 1148 | int rescan = 0; |
| 1149 | |
| 1150 | /* 1..N packets of ep->ep.maxpacket each ... the last one |
| 1151 | * may be short (including zero length). |
| 1152 | * |
| 1153 | * writer can send a zlp explicitly (length 0) or implicitly |
| 1154 | * (length mod maxpacket zero, and 'zero' flag); they always |
| 1155 | * terminate reads. |
| 1156 | */ |
| 1157 | host_len = urb->transfer_buffer_length - urb->actual_length; |
| 1158 | dev_len = req->req.length - req->req.actual; |
| 1159 | len = min (host_len, dev_len); |
| 1160 | |
| 1161 | /* FIXME update emulated data toggle too */ |
| 1162 | |
| 1163 | to_host = usb_pipein (urb->pipe); |
| 1164 | if (unlikely (len == 0)) |
| 1165 | is_short = 1; |
| 1166 | else { |
| 1167 | char *ubuf, *rbuf; |
| 1168 | |
| 1169 | /* not enough bandwidth left? */ |
| 1170 | if (limit < ep->ep.maxpacket && limit < len) |
| 1171 | break; |
| 1172 | len = min (len, (unsigned) limit); |
| 1173 | if (len == 0) |
| 1174 | break; |
| 1175 | |
| 1176 | /* use an extra pass for the final short packet */ |
| 1177 | if (len > ep->ep.maxpacket) { |
| 1178 | rescan = 1; |
| 1179 | len -= (len % ep->ep.maxpacket); |
| 1180 | } |
| 1181 | is_short = (len % ep->ep.maxpacket) != 0; |
| 1182 | |
| 1183 | /* else transfer packet(s) */ |
| 1184 | ubuf = urb->transfer_buffer + urb->actual_length; |
| 1185 | rbuf = req->req.buf + req->req.actual; |
| 1186 | if (to_host) |
| 1187 | memcpy (ubuf, rbuf, len); |
| 1188 | else |
| 1189 | memcpy (rbuf, ubuf, len); |
| 1190 | ep->last_io = jiffies; |
| 1191 | |
| 1192 | limit -= len; |
| 1193 | urb->actual_length += len; |
| 1194 | req->req.actual += len; |
| 1195 | } |
| 1196 | |
| 1197 | /* short packets terminate, maybe with overflow/underflow. |
| 1198 | * it's only really an error to write too much. |
| 1199 | * |
| 1200 | * partially filling a buffer optionally blocks queue advances |
| 1201 | * (so completion handlers can clean up the queue) but we don't |
Alan Stern | b0d9efb | 2007-08-21 15:39:21 -0400 | [diff] [blame] | 1202 | * need to emulate such data-in-flight. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | */ |
| 1204 | if (is_short) { |
| 1205 | if (host_len == dev_len) { |
| 1206 | req->req.status = 0; |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1207 | *status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } else if (to_host) { |
| 1209 | req->req.status = 0; |
| 1210 | if (dev_len > host_len) |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1211 | *status = -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | else |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1213 | *status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } else if (!to_host) { |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1215 | *status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | if (host_len > dev_len) |
| 1217 | req->req.status = -EOVERFLOW; |
| 1218 | else |
| 1219 | req->req.status = 0; |
| 1220 | } |
| 1221 | |
| 1222 | /* many requests terminate without a short packet */ |
| 1223 | } else { |
| 1224 | if (req->req.length == req->req.actual |
| 1225 | && !req->req.zero) |
| 1226 | req->req.status = 0; |
| 1227 | if (urb->transfer_buffer_length == urb->actual_length |
| 1228 | && !(urb->transfer_flags |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1229 | & URB_ZERO_PACKET)) |
| 1230 | *status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | /* device side completion --> continuable */ |
| 1234 | if (req->req.status != -EINPROGRESS) { |
| 1235 | list_del_init (&req->queue); |
| 1236 | |
| 1237 | spin_unlock (&dum->lock); |
| 1238 | req->req.complete (&ep->ep, &req->req); |
| 1239 | spin_lock (&dum->lock); |
| 1240 | |
| 1241 | /* requests might have been unlinked... */ |
| 1242 | rescan = 1; |
| 1243 | } |
| 1244 | |
| 1245 | /* host side completion --> terminate */ |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1246 | if (*status != -EINPROGRESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | break; |
| 1248 | |
| 1249 | /* rescan to continue with any other queued i/o */ |
| 1250 | if (rescan) |
| 1251 | goto top; |
| 1252 | } |
| 1253 | return limit; |
| 1254 | } |
| 1255 | |
| 1256 | static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep) |
| 1257 | { |
| 1258 | int limit = ep->ep.maxpacket; |
| 1259 | |
| 1260 | if (dum->gadget.speed == USB_SPEED_HIGH) { |
| 1261 | int tmp; |
| 1262 | |
| 1263 | /* high bandwidth mode */ |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 1264 | tmp = usb_endpoint_maxp(ep->desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | tmp = (tmp >> 11) & 0x03; |
| 1266 | tmp *= 8 /* applies to entire frame */; |
| 1267 | limit += limit * tmp; |
| 1268 | } |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1269 | if (dum->gadget.speed == USB_SPEED_SUPER) { |
| 1270 | switch (ep->desc->bmAttributes & 0x03) { |
| 1271 | case USB_ENDPOINT_XFER_ISOC: |
| 1272 | /* Sec. 4.4.8.2 USB3.0 Spec */ |
| 1273 | limit = 3 * 16 * 1024 * 8; |
| 1274 | break; |
| 1275 | case USB_ENDPOINT_XFER_INT: |
| 1276 | /* Sec. 4.4.7.2 USB3.0 Spec */ |
| 1277 | limit = 3 * 1024 * 8; |
| 1278 | break; |
| 1279 | case USB_ENDPOINT_XFER_BULK: |
| 1280 | default: |
| 1281 | break; |
| 1282 | } |
| 1283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | return limit; |
| 1285 | } |
| 1286 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1287 | #define is_active(dum_hcd) ((dum_hcd->port_status & \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \ |
| 1289 | USB_PORT_STAT_SUSPEND)) \ |
| 1290 | == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE)) |
| 1291 | |
| 1292 | static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address) |
| 1293 | { |
| 1294 | int i; |
| 1295 | |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1296 | if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ? |
| 1297 | dum->ss_hcd : dum->hs_hcd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | return NULL; |
| 1299 | if ((address & ~USB_DIR_IN) == 0) |
| 1300 | return &dum->ep [0]; |
| 1301 | for (i = 1; i < DUMMY_ENDPOINTS; i++) { |
| 1302 | struct dummy_ep *ep = &dum->ep [i]; |
| 1303 | |
| 1304 | if (!ep->desc) |
| 1305 | continue; |
| 1306 | if (ep->desc->bEndpointAddress == address) |
| 1307 | return ep; |
| 1308 | } |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | |
| 1312 | #undef is_active |
| 1313 | |
| 1314 | #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE) |
| 1315 | #define Dev_InRequest (Dev_Request | USB_DIR_IN) |
| 1316 | #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE) |
| 1317 | #define Intf_InRequest (Intf_Request | USB_DIR_IN) |
| 1318 | #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT) |
| 1319 | #define Ep_InRequest (Ep_Request | USB_DIR_IN) |
| 1320 | |
Tatyana Brokhman | 8be8a9d | 2010-11-01 17:38:05 +0200 | [diff] [blame] | 1321 | |
| 1322 | /** |
| 1323 | * handle_control_request() - handles all control transfers |
| 1324 | * @dum: pointer to dummy (the_controller) |
| 1325 | * @urb: the urb request to handle |
| 1326 | * @setup: pointer to the setup data for a USB device control |
| 1327 | * request |
| 1328 | * @status: pointer to request handling status |
| 1329 | * |
| 1330 | * Return 0 - if the request was handled |
| 1331 | * 1 - if the request wasn't handles |
| 1332 | * error code on error |
| 1333 | */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1334 | static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb, |
Tatyana Brokhman | 8be8a9d | 2010-11-01 17:38:05 +0200 | [diff] [blame] | 1335 | struct usb_ctrlrequest *setup, |
| 1336 | int *status) |
| 1337 | { |
| 1338 | struct dummy_ep *ep2; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1339 | struct dummy *dum = dum_hcd->dum; |
Tatyana Brokhman | 8be8a9d | 2010-11-01 17:38:05 +0200 | [diff] [blame] | 1340 | int ret_val = 1; |
| 1341 | unsigned w_index; |
| 1342 | unsigned w_value; |
| 1343 | |
| 1344 | w_index = le16_to_cpu(setup->wIndex); |
| 1345 | w_value = le16_to_cpu(setup->wValue); |
| 1346 | switch (setup->bRequest) { |
| 1347 | case USB_REQ_SET_ADDRESS: |
| 1348 | if (setup->bRequestType != Dev_Request) |
| 1349 | break; |
| 1350 | dum->address = w_value; |
| 1351 | *status = 0; |
| 1352 | dev_dbg(udc_dev(dum), "set_address = %d\n", |
| 1353 | w_value); |
| 1354 | ret_val = 0; |
| 1355 | break; |
| 1356 | case USB_REQ_SET_FEATURE: |
| 1357 | if (setup->bRequestType == Dev_Request) { |
| 1358 | ret_val = 0; |
| 1359 | switch (w_value) { |
| 1360 | case USB_DEVICE_REMOTE_WAKEUP: |
| 1361 | break; |
| 1362 | case USB_DEVICE_B_HNP_ENABLE: |
| 1363 | dum->gadget.b_hnp_enable = 1; |
| 1364 | break; |
| 1365 | case USB_DEVICE_A_HNP_SUPPORT: |
| 1366 | dum->gadget.a_hnp_support = 1; |
| 1367 | break; |
| 1368 | case USB_DEVICE_A_ALT_HNP_SUPPORT: |
| 1369 | dum->gadget.a_alt_hnp_support = 1; |
| 1370 | break; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1371 | case USB_DEVICE_U1_ENABLE: |
| 1372 | if (dummy_hcd_to_hcd(dum_hcd)->speed == |
| 1373 | HCD_USB3) |
| 1374 | w_value = USB_DEV_STAT_U1_ENABLED; |
| 1375 | else |
| 1376 | ret_val = -EOPNOTSUPP; |
| 1377 | break; |
| 1378 | case USB_DEVICE_U2_ENABLE: |
| 1379 | if (dummy_hcd_to_hcd(dum_hcd)->speed == |
| 1380 | HCD_USB3) |
| 1381 | w_value = USB_DEV_STAT_U2_ENABLED; |
| 1382 | else |
| 1383 | ret_val = -EOPNOTSUPP; |
| 1384 | break; |
| 1385 | case USB_DEVICE_LTM_ENABLE: |
| 1386 | if (dummy_hcd_to_hcd(dum_hcd)->speed == |
| 1387 | HCD_USB3) |
| 1388 | w_value = USB_DEV_STAT_LTM_ENABLED; |
| 1389 | else |
| 1390 | ret_val = -EOPNOTSUPP; |
| 1391 | break; |
Tatyana Brokhman | 8be8a9d | 2010-11-01 17:38:05 +0200 | [diff] [blame] | 1392 | default: |
| 1393 | ret_val = -EOPNOTSUPP; |
| 1394 | } |
| 1395 | if (ret_val == 0) { |
| 1396 | dum->devstatus |= (1 << w_value); |
| 1397 | *status = 0; |
| 1398 | } |
| 1399 | } else if (setup->bRequestType == Ep_Request) { |
| 1400 | /* endpoint halt */ |
| 1401 | ep2 = find_endpoint(dum, w_index); |
| 1402 | if (!ep2 || ep2->ep.name == ep0name) { |
| 1403 | ret_val = -EOPNOTSUPP; |
| 1404 | break; |
| 1405 | } |
| 1406 | ep2->halted = 1; |
| 1407 | ret_val = 0; |
| 1408 | *status = 0; |
| 1409 | } |
| 1410 | break; |
| 1411 | case USB_REQ_CLEAR_FEATURE: |
| 1412 | if (setup->bRequestType == Dev_Request) { |
| 1413 | ret_val = 0; |
| 1414 | switch (w_value) { |
| 1415 | case USB_DEVICE_REMOTE_WAKEUP: |
| 1416 | w_value = USB_DEVICE_REMOTE_WAKEUP; |
| 1417 | break; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1418 | case USB_DEVICE_U1_ENABLE: |
| 1419 | if (dummy_hcd_to_hcd(dum_hcd)->speed == |
| 1420 | HCD_USB3) |
| 1421 | w_value = USB_DEV_STAT_U1_ENABLED; |
| 1422 | else |
| 1423 | ret_val = -EOPNOTSUPP; |
| 1424 | break; |
| 1425 | case USB_DEVICE_U2_ENABLE: |
| 1426 | if (dummy_hcd_to_hcd(dum_hcd)->speed == |
| 1427 | HCD_USB3) |
| 1428 | w_value = USB_DEV_STAT_U2_ENABLED; |
| 1429 | else |
| 1430 | ret_val = -EOPNOTSUPP; |
| 1431 | break; |
| 1432 | case USB_DEVICE_LTM_ENABLE: |
| 1433 | if (dummy_hcd_to_hcd(dum_hcd)->speed == |
| 1434 | HCD_USB3) |
| 1435 | w_value = USB_DEV_STAT_LTM_ENABLED; |
| 1436 | else |
| 1437 | ret_val = -EOPNOTSUPP; |
| 1438 | break; |
Tatyana Brokhman | 8be8a9d | 2010-11-01 17:38:05 +0200 | [diff] [blame] | 1439 | default: |
| 1440 | ret_val = -EOPNOTSUPP; |
| 1441 | break; |
| 1442 | } |
| 1443 | if (ret_val == 0) { |
| 1444 | dum->devstatus &= ~(1 << w_value); |
| 1445 | *status = 0; |
| 1446 | } |
| 1447 | } else if (setup->bRequestType == Ep_Request) { |
| 1448 | /* endpoint halt */ |
| 1449 | ep2 = find_endpoint(dum, w_index); |
| 1450 | if (!ep2) { |
| 1451 | ret_val = -EOPNOTSUPP; |
| 1452 | break; |
| 1453 | } |
| 1454 | if (!ep2->wedged) |
| 1455 | ep2->halted = 0; |
| 1456 | ret_val = 0; |
| 1457 | *status = 0; |
| 1458 | } |
| 1459 | break; |
| 1460 | case USB_REQ_GET_STATUS: |
| 1461 | if (setup->bRequestType == Dev_InRequest |
| 1462 | || setup->bRequestType == Intf_InRequest |
| 1463 | || setup->bRequestType == Ep_InRequest) { |
| 1464 | char *buf; |
| 1465 | /* |
| 1466 | * device: remote wakeup, selfpowered |
| 1467 | * interface: nothing |
| 1468 | * endpoint: halt |
| 1469 | */ |
| 1470 | buf = (char *)urb->transfer_buffer; |
| 1471 | if (urb->transfer_buffer_length > 0) { |
| 1472 | if (setup->bRequestType == Ep_InRequest) { |
| 1473 | ep2 = find_endpoint(dum, w_index); |
| 1474 | if (!ep2) { |
| 1475 | ret_val = -EOPNOTSUPP; |
| 1476 | break; |
| 1477 | } |
| 1478 | buf[0] = ep2->halted; |
| 1479 | } else if (setup->bRequestType == |
| 1480 | Dev_InRequest) { |
| 1481 | buf[0] = (u8)dum->devstatus; |
| 1482 | } else |
| 1483 | buf[0] = 0; |
| 1484 | } |
| 1485 | if (urb->transfer_buffer_length > 1) |
| 1486 | buf[1] = 0; |
| 1487 | urb->actual_length = min_t(u32, 2, |
| 1488 | urb->transfer_buffer_length); |
| 1489 | ret_val = 0; |
| 1490 | *status = 0; |
| 1491 | } |
| 1492 | break; |
| 1493 | } |
| 1494 | return ret_val; |
| 1495 | } |
| 1496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | /* drive both sides of the transfers; looks like irq handlers to |
| 1498 | * both drivers except the callbacks aren't in_irq(). |
| 1499 | */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1500 | static void dummy_timer(unsigned long _dum_hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1502 | struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd; |
| 1503 | struct dummy *dum = dum_hcd->dum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | struct urbp *urbp, *tmp; |
| 1505 | unsigned long flags; |
| 1506 | int limit, total; |
| 1507 | int i; |
| 1508 | |
| 1509 | /* simplistic model for one frame's bandwidth */ |
| 1510 | switch (dum->gadget.speed) { |
| 1511 | case USB_SPEED_LOW: |
| 1512 | total = 8/*bytes*/ * 12/*packets*/; |
| 1513 | break; |
| 1514 | case USB_SPEED_FULL: |
| 1515 | total = 64/*bytes*/ * 19/*packets*/; |
| 1516 | break; |
| 1517 | case USB_SPEED_HIGH: |
| 1518 | total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/; |
| 1519 | break; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1520 | case USB_SPEED_SUPER: |
| 1521 | /* Bus speed is 500000 bytes/ms, so use a little less */ |
| 1522 | total = 490000; |
| 1523 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | default: |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1525 | dev_err(dummy_dev(dum_hcd), "bogus device speed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | return; |
| 1527 | } |
| 1528 | |
| 1529 | /* FIXME if HZ != 1000 this will probably misbehave ... */ |
| 1530 | |
| 1531 | /* look at each urb queued by the host side driver */ |
| 1532 | spin_lock_irqsave (&dum->lock, flags); |
| 1533 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1534 | if (!dum_hcd->udev) { |
| 1535 | dev_err(dummy_dev(dum_hcd), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | "timer fired with no URBs pending?\n"); |
| 1537 | spin_unlock_irqrestore (&dum->lock, flags); |
| 1538 | return; |
| 1539 | } |
| 1540 | |
| 1541 | for (i = 0; i < DUMMY_ENDPOINTS; i++) { |
| 1542 | if (!ep_name [i]) |
| 1543 | break; |
| 1544 | dum->ep [i].already_seen = 0; |
| 1545 | } |
| 1546 | |
| 1547 | restart: |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1548 | list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | struct urb *urb; |
| 1550 | struct dummy_request *req; |
| 1551 | u8 address; |
| 1552 | struct dummy_ep *ep = NULL; |
| 1553 | int type; |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1554 | int status = -EINPROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
| 1556 | urb = urbp->urb; |
Alan Stern | eb23105 | 2007-08-21 15:40:36 -0400 | [diff] [blame] | 1557 | if (urb->unlinked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | goto return_urb; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1559 | else if (dum_hcd->rh_state != DUMMY_RH_RUNNING) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1560 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | type = usb_pipetype (urb->pipe); |
| 1562 | |
| 1563 | /* used up this frame's non-periodic bandwidth? |
| 1564 | * FIXME there's infinite bandwidth for control and |
| 1565 | * periodic transfers ... unrealistic. |
| 1566 | */ |
| 1567 | if (total <= 0 && type == PIPE_BULK) |
| 1568 | continue; |
| 1569 | |
| 1570 | /* find the gadget's ep for this request (if configured) */ |
| 1571 | address = usb_pipeendpoint (urb->pipe); |
| 1572 | if (usb_pipein (urb->pipe)) |
| 1573 | address |= USB_DIR_IN; |
| 1574 | ep = find_endpoint(dum, address); |
| 1575 | if (!ep) { |
| 1576 | /* set_configuration() disagreement */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1577 | dev_dbg(dummy_dev(dum_hcd), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | "no ep configured for urb %p\n", |
| 1579 | urb); |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1580 | status = -EPROTO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | goto return_urb; |
| 1582 | } |
| 1583 | |
| 1584 | if (ep->already_seen) |
| 1585 | continue; |
| 1586 | ep->already_seen = 1; |
| 1587 | if (ep == &dum->ep [0] && urb->error_count) { |
| 1588 | ep->setup_stage = 1; /* a new urb */ |
| 1589 | urb->error_count = 0; |
| 1590 | } |
| 1591 | if (ep->halted && !ep->setup_stage) { |
| 1592 | /* NOTE: must not be iso! */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1593 | dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | ep->ep.name, urb); |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1595 | status = -EPIPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | goto return_urb; |
| 1597 | } |
| 1598 | /* FIXME make sure both ends agree on maxpacket */ |
| 1599 | |
| 1600 | /* handle control requests */ |
| 1601 | if (ep == &dum->ep [0] && ep->setup_stage) { |
| 1602 | struct usb_ctrlrequest setup; |
| 1603 | int value = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
| 1605 | setup = *(struct usb_ctrlrequest*) urb->setup_packet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | /* paranoia, in case of stale queued data */ |
| 1607 | list_for_each_entry (req, &ep->queue, queue) { |
| 1608 | list_del_init (&req->queue); |
| 1609 | req->req.status = -EOVERFLOW; |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 1610 | dev_dbg (udc_dev(dum), "stale req = %p\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | req); |
| 1612 | |
| 1613 | spin_unlock (&dum->lock); |
| 1614 | req->req.complete (&ep->ep, &req->req); |
| 1615 | spin_lock (&dum->lock); |
| 1616 | ep->already_seen = 0; |
| 1617 | goto restart; |
| 1618 | } |
| 1619 | |
| 1620 | /* gadget driver never sees set_address or operations |
| 1621 | * on standard feature flags. some hardware doesn't |
| 1622 | * even expose them. |
| 1623 | */ |
| 1624 | ep->last_io = jiffies; |
| 1625 | ep->setup_stage = 0; |
| 1626 | ep->halted = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1628 | value = handle_control_request(dum_hcd, urb, &setup, |
Tatyana Brokhman | 8be8a9d | 2010-11-01 17:38:05 +0200 | [diff] [blame] | 1629 | &status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
| 1631 | /* gadget driver handles all other requests. block |
| 1632 | * until setup() returns; no reentrancy issues etc. |
| 1633 | */ |
| 1634 | if (value > 0) { |
| 1635 | spin_unlock (&dum->lock); |
| 1636 | value = dum->driver->setup (&dum->gadget, |
| 1637 | &setup); |
| 1638 | spin_lock (&dum->lock); |
| 1639 | |
| 1640 | if (value >= 0) { |
| 1641 | /* no delays (max 64KB data stage) */ |
| 1642 | limit = 64*1024; |
| 1643 | goto treat_control_like_bulk; |
| 1644 | } |
| 1645 | /* error, see below */ |
| 1646 | } |
| 1647 | |
| 1648 | if (value < 0) { |
| 1649 | if (value != -EOPNOTSUPP) |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 1650 | dev_dbg (udc_dev(dum), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | "setup --> %d\n", |
| 1652 | value); |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1653 | status = -EPIPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | urb->actual_length = 0; |
| 1655 | } |
| 1656 | |
| 1657 | goto return_urb; |
| 1658 | } |
| 1659 | |
| 1660 | /* non-control requests */ |
| 1661 | limit = total; |
| 1662 | switch (usb_pipetype (urb->pipe)) { |
| 1663 | case PIPE_ISOCHRONOUS: |
| 1664 | /* FIXME is it urb->interval since the last xfer? |
| 1665 | * use urb->iso_frame_desc[i]. |
| 1666 | * complete whether or not ep has requests queued. |
| 1667 | * report random errors, to debug drivers. |
| 1668 | */ |
| 1669 | limit = max (limit, periodic_bytes (dum, ep)); |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1670 | status = -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | break; |
| 1672 | |
| 1673 | case PIPE_INTERRUPT: |
| 1674 | /* FIXME is it urb->interval since the last xfer? |
| 1675 | * this almost certainly polls too fast. |
| 1676 | */ |
| 1677 | limit = max (limit, periodic_bytes (dum, ep)); |
| 1678 | /* FALLTHROUGH */ |
| 1679 | |
| 1680 | // case PIPE_BULK: case PIPE_CONTROL: |
| 1681 | default: |
| 1682 | treat_control_like_bulk: |
| 1683 | ep->last_io = jiffies; |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1684 | total = transfer(dum, urb, ep, limit, &status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | /* incomplete transfer? */ |
Alan Stern | 4d2f110 | 2007-08-24 15:40:10 -0400 | [diff] [blame] | 1689 | if (status == -EINPROGRESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | continue; |
| 1691 | |
| 1692 | return_urb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | list_del (&urbp->urbp_list); |
| 1694 | kfree (urbp); |
| 1695 | if (ep) |
| 1696 | ep->already_seen = ep->setup_stage = 0; |
| 1697 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1698 | usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | spin_unlock (&dum->lock); |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1700 | usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | spin_lock (&dum->lock); |
| 1702 | |
| 1703 | goto restart; |
| 1704 | } |
| 1705 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1706 | if (list_empty(&dum_hcd->urbp_list)) { |
| 1707 | usb_put_dev(dum_hcd->udev); |
| 1708 | dum_hcd->udev = NULL; |
| 1709 | } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) { |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1710 | /* want a 1 msec delay here */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1711 | mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | spin_unlock_irqrestore (&dum->lock, flags); |
| 1715 | } |
| 1716 | |
| 1717 | /*-------------------------------------------------------------------------*/ |
| 1718 | |
| 1719 | #define PORT_C_MASK \ |
Alan Stern | c2db8b5 | 2005-04-29 16:30:48 -0400 | [diff] [blame] | 1720 | ((USB_PORT_STAT_C_CONNECTION \ |
| 1721 | | USB_PORT_STAT_C_ENABLE \ |
| 1722 | | USB_PORT_STAT_C_SUSPEND \ |
| 1723 | | USB_PORT_STAT_C_OVERCURRENT \ |
| 1724 | | USB_PORT_STAT_C_RESET) << 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
| 1726 | static int dummy_hub_status (struct usb_hcd *hcd, char *buf) |
| 1727 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1728 | struct dummy_hcd *dum_hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | unsigned long flags; |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1730 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1732 | dum_hcd = hcd_to_dummy_hcd(hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1734 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); |
Alan Stern | 541c7d4 | 2010-06-22 16:39:10 -0400 | [diff] [blame] | 1735 | if (!HCD_HW_ACCESSIBLE(hcd)) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1736 | goto done; |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1737 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1738 | if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) { |
| 1739 | dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16); |
| 1740 | dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND; |
| 1741 | set_link_state(dum_hcd); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1742 | } |
| 1743 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1744 | if ((dum_hcd->port_status & PORT_C_MASK) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | *buf = (1 << 1); |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1746 | dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n", |
| 1747 | dum_hcd->port_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | retval = 1; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1749 | if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1750 | usb_hcd_resume_root_hub (hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | } |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1752 | done: |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1753 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | return retval; |
| 1755 | } |
| 1756 | |
| 1757 | static inline void |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1758 | ss_hub_descriptor(struct usb_hub_descriptor *desc) |
| 1759 | { |
| 1760 | memset(desc, 0, sizeof *desc); |
| 1761 | desc->bDescriptorType = 0x2a; |
| 1762 | desc->bDescLength = 12; |
| 1763 | desc->wHubCharacteristics = cpu_to_le16(0x0001); |
| 1764 | desc->bNbrPorts = 1; |
| 1765 | desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/ |
| 1766 | desc->u.ss.DeviceRemovable = 0xffff; |
| 1767 | } |
| 1768 | |
| 1769 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | hub_descriptor (struct usb_hub_descriptor *desc) |
| 1771 | { |
| 1772 | memset (desc, 0, sizeof *desc); |
| 1773 | desc->bDescriptorType = 0x29; |
| 1774 | desc->bDescLength = 9; |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 1775 | desc->wHubCharacteristics = cpu_to_le16(0x0001); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | desc->bNbrPorts = 1; |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 1777 | desc->u.hs.DeviceRemovable[0] = 0xff; |
| 1778 | desc->u.hs.DeviceRemovable[1] = 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | static int dummy_hub_control ( |
| 1782 | struct usb_hcd *hcd, |
| 1783 | u16 typeReq, |
| 1784 | u16 wValue, |
| 1785 | u16 wIndex, |
| 1786 | char *buf, |
| 1787 | u16 wLength |
| 1788 | ) { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1789 | struct dummy_hcd *dum_hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | int retval = 0; |
| 1791 | unsigned long flags; |
| 1792 | |
Alan Stern | 541c7d4 | 2010-06-22 16:39:10 -0400 | [diff] [blame] | 1793 | if (!HCD_HW_ACCESSIBLE(hcd)) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 1794 | return -ETIMEDOUT; |
| 1795 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1796 | dum_hcd = hcd_to_dummy_hcd(hcd); |
| 1797 | |
| 1798 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | switch (typeReq) { |
| 1800 | case ClearHubFeature: |
| 1801 | break; |
| 1802 | case ClearPortFeature: |
| 1803 | switch (wValue) { |
| 1804 | case USB_PORT_FEAT_SUSPEND: |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1805 | if (hcd->speed == HCD_USB3) { |
| 1806 | dev_dbg(dummy_dev(dum_hcd), |
| 1807 | "USB_PORT_FEAT_SUSPEND req not " |
| 1808 | "supported for USB 3.0 roothub\n"); |
| 1809 | goto error; |
| 1810 | } |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1811 | if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | /* 20msec resume signaling */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1813 | dum_hcd->resuming = 1; |
| 1814 | dum_hcd->re_timeout = jiffies + |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1815 | msecs_to_jiffies(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | } |
| 1817 | break; |
| 1818 | case USB_PORT_FEAT_POWER: |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1819 | if (hcd->speed == HCD_USB3) { |
| 1820 | if (dum_hcd->port_status & USB_PORT_STAT_POWER) |
| 1821 | dev_dbg(dummy_dev(dum_hcd), |
| 1822 | "power-off\n"); |
| 1823 | } else |
| 1824 | if (dum_hcd->port_status & |
| 1825 | USB_SS_PORT_STAT_POWER) |
| 1826 | dev_dbg(dummy_dev(dum_hcd), |
| 1827 | "power-off\n"); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1828 | /* FALLS THROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | default: |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1830 | dum_hcd->port_status &= ~(1 << wValue); |
| 1831 | set_link_state(dum_hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | } |
| 1833 | break; |
| 1834 | case GetHubDescriptor: |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1835 | if (hcd->speed == HCD_USB3 && |
| 1836 | (wLength < USB_DT_SS_HUB_SIZE || |
| 1837 | wValue != (USB_DT_SS_HUB << 8))) { |
| 1838 | dev_dbg(dummy_dev(dum_hcd), |
| 1839 | "Wrong hub descriptor type for " |
| 1840 | "USB 3.0 roothub.\n"); |
| 1841 | goto error; |
| 1842 | } |
| 1843 | if (hcd->speed == HCD_USB3) |
| 1844 | ss_hub_descriptor((struct usb_hub_descriptor *) buf); |
| 1845 | else |
| 1846 | hub_descriptor((struct usb_hub_descriptor *) buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | break; |
| 1848 | case GetHubStatus: |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1849 | *(__le32 *) buf = cpu_to_le32 (0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | break; |
| 1851 | case GetPortStatus: |
| 1852 | if (wIndex != 1) |
| 1853 | retval = -EPIPE; |
| 1854 | |
| 1855 | /* whoever resets or resumes must GetPortStatus to |
| 1856 | * complete it!! |
| 1857 | */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1858 | if (dum_hcd->resuming && |
| 1859 | time_after_eq(jiffies, dum_hcd->re_timeout)) { |
| 1860 | dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16); |
| 1861 | dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | } |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1863 | if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 && |
| 1864 | time_after_eq(jiffies, dum_hcd->re_timeout)) { |
| 1865 | dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16); |
| 1866 | dum_hcd->port_status &= ~USB_PORT_STAT_RESET; |
| 1867 | if (dum_hcd->dum->pullup) { |
| 1868 | dum_hcd->port_status |= USB_PORT_STAT_ENABLE; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1869 | |
| 1870 | if (hcd->speed < HCD_USB3) { |
| 1871 | switch (dum_hcd->dum->gadget.speed) { |
| 1872 | case USB_SPEED_HIGH: |
| 1873 | dum_hcd->port_status |= |
| 1874 | USB_PORT_STAT_HIGH_SPEED; |
| 1875 | break; |
| 1876 | case USB_SPEED_LOW: |
| 1877 | dum_hcd->dum->gadget.ep0-> |
| 1878 | maxpacket = 8; |
| 1879 | dum_hcd->port_status |= |
| 1880 | USB_PORT_STAT_LOW_SPEED; |
| 1881 | break; |
| 1882 | default: |
| 1883 | dum_hcd->dum->gadget.speed = |
| 1884 | USB_SPEED_FULL; |
| 1885 | break; |
| 1886 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | } |
| 1888 | } |
| 1889 | } |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1890 | set_link_state(dum_hcd); |
| 1891 | ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status); |
| 1892 | ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | break; |
| 1894 | case SetHubFeature: |
| 1895 | retval = -EPIPE; |
| 1896 | break; |
| 1897 | case SetPortFeature: |
| 1898 | switch (wValue) { |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1899 | case USB_PORT_FEAT_LINK_STATE: |
| 1900 | if (hcd->speed != HCD_USB3) { |
| 1901 | dev_dbg(dummy_dev(dum_hcd), |
| 1902 | "USB_PORT_FEAT_LINK_STATE req not " |
| 1903 | "supported for USB 2.0 roothub\n"); |
| 1904 | goto error; |
| 1905 | } |
| 1906 | /* |
| 1907 | * Since this is dummy we don't have an actual link so |
| 1908 | * there is nothing to do for the SET_LINK_STATE cmd |
| 1909 | */ |
| 1910 | break; |
| 1911 | case USB_PORT_FEAT_U1_TIMEOUT: |
| 1912 | case USB_PORT_FEAT_U2_TIMEOUT: |
| 1913 | /* TODO: add suspend/resume support! */ |
| 1914 | if (hcd->speed != HCD_USB3) { |
| 1915 | dev_dbg(dummy_dev(dum_hcd), |
| 1916 | "USB_PORT_FEAT_U1/2_TIMEOUT req not " |
| 1917 | "supported for USB 2.0 roothub\n"); |
| 1918 | goto error; |
| 1919 | } |
| 1920 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | case USB_PORT_FEAT_SUSPEND: |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1922 | /* Applicable only for USB2.0 hub */ |
| 1923 | if (hcd->speed == HCD_USB3) { |
| 1924 | dev_dbg(dummy_dev(dum_hcd), |
| 1925 | "USB_PORT_FEAT_SUSPEND req not " |
| 1926 | "supported for USB 3.0 roothub\n"); |
| 1927 | goto error; |
| 1928 | } |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1929 | if (dum_hcd->active) { |
| 1930 | dum_hcd->port_status |= USB_PORT_STAT_SUSPEND; |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1931 | |
| 1932 | /* HNP would happen here; for now we |
| 1933 | * assume b_bus_req is always true. |
| 1934 | */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1935 | set_link_state(dum_hcd); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1936 | if (((1 << USB_DEVICE_B_HNP_ENABLE) |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1937 | & dum_hcd->dum->devstatus) != 0) |
| 1938 | dev_dbg(dummy_dev(dum_hcd), |
Alan Stern | 5742b0c | 2005-05-02 11:25:17 -0400 | [diff] [blame] | 1939 | "no HNP yet!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | } |
| 1941 | break; |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1942 | case USB_PORT_FEAT_POWER: |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1943 | if (hcd->speed == HCD_USB3) |
| 1944 | dum_hcd->port_status |= USB_SS_PORT_STAT_POWER; |
| 1945 | else |
| 1946 | dum_hcd->port_status |= USB_PORT_STAT_POWER; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1947 | set_link_state(dum_hcd); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1948 | break; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1949 | case USB_PORT_FEAT_BH_PORT_RESET: |
| 1950 | /* Applicable only for USB3.0 hub */ |
| 1951 | if (hcd->speed != HCD_USB3) { |
| 1952 | dev_dbg(dummy_dev(dum_hcd), |
| 1953 | "USB_PORT_FEAT_BH_PORT_RESET req not " |
| 1954 | "supported for USB 2.0 roothub\n"); |
| 1955 | goto error; |
| 1956 | } |
| 1957 | /* FALLS THROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | case USB_PORT_FEAT_RESET: |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1959 | /* if it's already enabled, disable */ |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1960 | if (hcd->speed == HCD_USB3) { |
| 1961 | dum_hcd->port_status = 0; |
| 1962 | dum_hcd->port_status = |
| 1963 | (USB_SS_PORT_STAT_POWER | |
| 1964 | USB_PORT_STAT_CONNECTION | |
| 1965 | USB_PORT_STAT_RESET); |
| 1966 | } else |
| 1967 | dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1968 | | USB_PORT_STAT_LOW_SPEED |
| 1969 | | USB_PORT_STAT_HIGH_SPEED); |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1970 | /* |
| 1971 | * We want to reset device status. All but the |
| 1972 | * Self powered feature |
| 1973 | */ |
| 1974 | dum_hcd->dum->devstatus &= |
| 1975 | (1 << USB_DEVICE_SELF_POWERED); |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1976 | /* |
| 1977 | * FIXME USB3.0: what is the correct reset signaling |
| 1978 | * interval? Is it still 50msec as for HS? |
| 1979 | */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 1980 | dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50); |
Alan Stern | f1c39fa | 2005-05-03 16:24:04 -0400 | [diff] [blame] | 1981 | /* FALLS THROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | default: |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1983 | if (hcd->speed == HCD_USB3) { |
| 1984 | if ((dum_hcd->port_status & |
| 1985 | USB_SS_PORT_STAT_POWER) != 0) { |
| 1986 | dum_hcd->port_status |= (1 << wValue); |
| 1987 | set_link_state(dum_hcd); |
| 1988 | } |
| 1989 | } else |
| 1990 | if ((dum_hcd->port_status & |
| 1991 | USB_PORT_STAT_POWER) != 0) { |
| 1992 | dum_hcd->port_status |= (1 << wValue); |
| 1993 | set_link_state(dum_hcd); |
| 1994 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | } |
| 1996 | break; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 1997 | case GetPortErrorCount: |
| 1998 | if (hcd->speed != HCD_USB3) { |
| 1999 | dev_dbg(dummy_dev(dum_hcd), |
| 2000 | "GetPortErrorCount req not " |
| 2001 | "supported for USB 2.0 roothub\n"); |
| 2002 | goto error; |
| 2003 | } |
| 2004 | /* We'll always return 0 since this is a dummy hub */ |
| 2005 | *(__le32 *) buf = cpu_to_le32(0); |
| 2006 | break; |
| 2007 | case SetHubDepth: |
| 2008 | if (hcd->speed != HCD_USB3) { |
| 2009 | dev_dbg(dummy_dev(dum_hcd), |
| 2010 | "SetHubDepth req not supported for " |
| 2011 | "USB 2.0 roothub\n"); |
| 2012 | goto error; |
| 2013 | } |
| 2014 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | default: |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2016 | dev_dbg(dummy_dev(dum_hcd), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | "hub control req%04x v%04x i%04x l%d\n", |
| 2018 | typeReq, wValue, wIndex, wLength); |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2019 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | /* "protocol stall" on error */ |
| 2021 | retval = -EPIPE; |
| 2022 | } |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2023 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
Alan Stern | 685eb93 | 2005-05-03 16:27:26 -0400 | [diff] [blame] | 2024 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2025 | if ((dum_hcd->port_status & PORT_C_MASK) != 0) |
Alan Stern | 685eb93 | 2005-05-03 16:27:26 -0400 | [diff] [blame] | 2026 | usb_hcd_poll_rh_status (hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | return retval; |
| 2028 | } |
| 2029 | |
Alan Stern | 0c0382e | 2005-10-13 17:08:02 -0400 | [diff] [blame] | 2030 | static int dummy_bus_suspend (struct usb_hcd *hcd) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2031 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2032 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2033 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2034 | dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__); |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2035 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2036 | spin_lock_irq(&dum_hcd->dum->lock); |
| 2037 | dum_hcd->rh_state = DUMMY_RH_SUSPENDED; |
| 2038 | set_link_state(dum_hcd); |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2039 | hcd->state = HC_STATE_SUSPENDED; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2040 | spin_unlock_irq(&dum_hcd->dum->lock); |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2041 | return 0; |
| 2042 | } |
| 2043 | |
Alan Stern | 0c0382e | 2005-10-13 17:08:02 -0400 | [diff] [blame] | 2044 | static int dummy_bus_resume (struct usb_hcd *hcd) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2045 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2046 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2047 | int rc = 0; |
| 2048 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2049 | dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__); |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2050 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2051 | spin_lock_irq(&dum_hcd->dum->lock); |
Alan Stern | 541c7d4 | 2010-06-22 16:39:10 -0400 | [diff] [blame] | 2052 | if (!HCD_HW_ACCESSIBLE(hcd)) { |
Alan Stern | cfa59da | 2007-06-21 16:25:35 -0400 | [diff] [blame] | 2053 | rc = -ESHUTDOWN; |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2054 | } else { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2055 | dum_hcd->rh_state = DUMMY_RH_RUNNING; |
| 2056 | set_link_state(dum_hcd); |
| 2057 | if (!list_empty(&dum_hcd->urbp_list)) |
| 2058 | mod_timer(&dum_hcd->timer, jiffies); |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2059 | hcd->state = HC_STATE_RUNNING; |
| 2060 | } |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2061 | spin_unlock_irq(&dum_hcd->dum->lock); |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2062 | return rc; |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | |
| 2065 | /*-------------------------------------------------------------------------*/ |
| 2066 | |
| 2067 | static inline ssize_t |
| 2068 | show_urb (char *buf, size_t size, struct urb *urb) |
| 2069 | { |
| 2070 | int ep = usb_pipeendpoint (urb->pipe); |
| 2071 | |
| 2072 | return snprintf (buf, size, |
| 2073 | "urb/%p %s ep%d%s%s len %d/%d\n", |
| 2074 | urb, |
| 2075 | ({ char *s; |
| 2076 | switch (urb->dev->speed) { |
Tatyana Brokhman | 7c884fe | 2011-06-28 16:33:52 +0300 | [diff] [blame] | 2077 | case USB_SPEED_LOW: |
| 2078 | s = "ls"; |
| 2079 | break; |
| 2080 | case USB_SPEED_FULL: |
| 2081 | s = "fs"; |
| 2082 | break; |
| 2083 | case USB_SPEED_HIGH: |
| 2084 | s = "hs"; |
| 2085 | break; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2086 | case USB_SPEED_SUPER: |
| 2087 | s = "ss"; |
| 2088 | break; |
Tatyana Brokhman | 7c884fe | 2011-06-28 16:33:52 +0300 | [diff] [blame] | 2089 | default: |
| 2090 | s = "?"; |
| 2091 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | }; s; }), |
| 2093 | ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "", |
| 2094 | ({ char *s; \ |
| 2095 | switch (usb_pipetype (urb->pipe)) { \ |
Tatyana Brokhman | 7c884fe | 2011-06-28 16:33:52 +0300 | [diff] [blame] | 2096 | case PIPE_CONTROL: \ |
| 2097 | s = ""; \ |
| 2098 | break; \ |
| 2099 | case PIPE_BULK: \ |
| 2100 | s = "-bulk"; \ |
| 2101 | break; \ |
| 2102 | case PIPE_INTERRUPT: \ |
| 2103 | s = "-int"; \ |
| 2104 | break; \ |
| 2105 | default: \ |
| 2106 | s = "-iso"; \ |
| 2107 | break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | }; s;}), |
| 2109 | urb->actual_length, urb->transfer_buffer_length); |
| 2110 | } |
| 2111 | |
| 2112 | static ssize_t |
Yani Ioannou | 10523b3 | 2005-05-17 06:43:37 -0400 | [diff] [blame] | 2113 | show_urbs (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | { |
| 2115 | struct usb_hcd *hcd = dev_get_drvdata (dev); |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2116 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | struct urbp *urbp; |
| 2118 | size_t size = 0; |
| 2119 | unsigned long flags; |
| 2120 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2121 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); |
| 2122 | list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | size_t temp; |
| 2124 | |
| 2125 | temp = show_urb (buf, PAGE_SIZE - size, urbp->urb); |
| 2126 | buf += temp; |
| 2127 | size += temp; |
| 2128 | } |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2129 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | |
| 2131 | return size; |
| 2132 | } |
| 2133 | static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL); |
| 2134 | |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2135 | static int dummy_start_ss(struct dummy_hcd *dum_hcd) |
| 2136 | { |
| 2137 | init_timer(&dum_hcd->timer); |
| 2138 | dum_hcd->timer.function = dummy_timer; |
| 2139 | dum_hcd->timer.data = (unsigned long)dum_hcd; |
| 2140 | dum_hcd->rh_state = DUMMY_RH_RUNNING; |
| 2141 | INIT_LIST_HEAD(&dum_hcd->urbp_list); |
| 2142 | dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET; |
| 2143 | dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING; |
| 2144 | dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1; |
| 2145 | #ifdef CONFIG_USB_OTG |
| 2146 | dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1; |
| 2147 | #endif |
| 2148 | return 0; |
| 2149 | |
| 2150 | /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */ |
| 2151 | return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs); |
| 2152 | } |
| 2153 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2154 | static int dummy_start(struct usb_hcd *hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2156 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
| 2158 | /* |
| 2159 | * MASTER side init ... we emulate a root hub that'll only ever |
| 2160 | * talk to one device (the slave side). Also appears in sysfs, |
| 2161 | * just like more familiar pci-based HCDs. |
| 2162 | */ |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2163 | if (!usb_hcd_is_primary_hcd(hcd)) |
| 2164 | return dummy_start_ss(dum_hcd); |
| 2165 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2166 | spin_lock_init(&dum_hcd->dum->lock); |
| 2167 | init_timer(&dum_hcd->timer); |
| 2168 | dum_hcd->timer.function = dummy_timer; |
| 2169 | dum_hcd->timer.data = (unsigned long)dum_hcd; |
| 2170 | dum_hcd->rh_state = DUMMY_RH_RUNNING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2172 | INIT_LIST_HEAD(&dum_hcd->urbp_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | |
Alan Stern | caf29f6 | 2007-12-06 11:10:39 -0500 | [diff] [blame] | 2174 | hcd->power_budget = POWER_BUDGET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | hcd->state = HC_STATE_RUNNING; |
Alan Stern | 685eb93 | 2005-05-03 16:27:26 -0400 | [diff] [blame] | 2176 | hcd->uses_new_polling = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | |
Alan Stern | 5742b0c | 2005-05-02 11:25:17 -0400 | [diff] [blame] | 2178 | #ifdef CONFIG_USB_OTG |
| 2179 | hcd->self.otg_port = 1; |
| 2180 | #endif |
| 2181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2183 | return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | static void dummy_stop (struct usb_hcd *hcd) |
| 2187 | { |
| 2188 | struct dummy *dum; |
| 2189 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2190 | dum = (hcd_to_dummy_hcd(hcd))->dum; |
| 2191 | device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs); |
| 2192 | usb_gadget_unregister_driver(dum->driver); |
| 2193 | dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | /*-------------------------------------------------------------------------*/ |
| 2197 | |
| 2198 | static int dummy_h_get_frame (struct usb_hcd *hcd) |
| 2199 | { |
| 2200 | return dummy_g_get_frame (NULL); |
| 2201 | } |
| 2202 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2203 | static int dummy_setup(struct usb_hcd *hcd) |
| 2204 | { |
| 2205 | if (usb_hcd_is_primary_hcd(hcd)) { |
| 2206 | the_controller.hs_hcd = hcd_to_dummy_hcd(hcd); |
| 2207 | the_controller.hs_hcd->dum = &the_controller; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2208 | /* |
| 2209 | * Mark the first roothub as being USB 2.0. |
| 2210 | * The USB 3.0 roothub will be registered later by |
| 2211 | * dummy_hcd_probe() |
| 2212 | */ |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2213 | hcd->speed = HCD_USB2; |
| 2214 | hcd->self.root_hub->speed = USB_SPEED_HIGH; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2215 | } else { |
| 2216 | the_controller.ss_hcd = hcd_to_dummy_hcd(hcd); |
| 2217 | the_controller.ss_hcd->dum = &the_controller; |
| 2218 | hcd->speed = HCD_USB3; |
| 2219 | hcd->self.root_hub->speed = USB_SPEED_SUPER; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2220 | } |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2224 | /* Change a group of bulk endpoints to support multiple stream IDs */ |
| 2225 | int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev, |
| 2226 | struct usb_host_endpoint **eps, unsigned int num_eps, |
| 2227 | unsigned int num_streams, gfp_t mem_flags) |
| 2228 | { |
| 2229 | if (hcd->speed != HCD_USB3) |
| 2230 | dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)), |
| 2231 | "%s() - ERROR! Not supported for USB2.0 roothub\n", |
| 2232 | __func__); |
| 2233 | return 0; |
| 2234 | } |
| 2235 | |
| 2236 | /* Reverts a group of bulk endpoints back to not using stream IDs. */ |
| 2237 | int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev, |
| 2238 | struct usb_host_endpoint **eps, unsigned int num_eps, |
| 2239 | gfp_t mem_flags) |
| 2240 | { |
| 2241 | if (hcd->speed != HCD_USB3) |
| 2242 | dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)), |
| 2243 | "%s() - ERROR! Not supported for USB2.0 roothub\n", |
| 2244 | __func__); |
| 2245 | return 0; |
| 2246 | } |
| 2247 | |
| 2248 | static struct hc_driver dummy_hcd = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | .description = (char *) driver_name, |
| 2250 | .product_desc = "Dummy host controller", |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2251 | .hcd_priv_size = sizeof(struct dummy_hcd), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2253 | .flags = HCD_USB3 | HCD_SHARED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2255 | .reset = dummy_setup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | .start = dummy_start, |
| 2257 | .stop = dummy_stop, |
| 2258 | |
| 2259 | .urb_enqueue = dummy_urb_enqueue, |
| 2260 | .urb_dequeue = dummy_urb_dequeue, |
| 2261 | |
| 2262 | .get_frame_number = dummy_h_get_frame, |
| 2263 | |
| 2264 | .hub_status_data = dummy_hub_status, |
| 2265 | .hub_control = dummy_hub_control, |
Alan Stern | 0c0382e | 2005-10-13 17:08:02 -0400 | [diff] [blame] | 2266 | .bus_suspend = dummy_bus_suspend, |
| 2267 | .bus_resume = dummy_bus_resume, |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2268 | |
| 2269 | .alloc_streams = dummy_alloc_streams, |
| 2270 | .free_streams = dummy_free_streams, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | }; |
| 2272 | |
Alan Stern | 8364d6b | 2005-11-14 12:16:30 -0500 | [diff] [blame] | 2273 | static int dummy_hcd_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2275 | struct usb_hcd *hs_hcd; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2276 | struct usb_hcd *ss_hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | int retval; |
| 2278 | |
Alan Stern | 8364d6b | 2005-11-14 12:16:30 -0500 | [diff] [blame] | 2279 | dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2281 | if (!mod_data.is_super_speed) |
| 2282 | dummy_hcd.flags = HCD_USB2; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2283 | hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev)); |
| 2284 | if (!hs_hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | return -ENOMEM; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2286 | hs_hcd->has_tt = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2288 | retval = usb_add_hcd(hs_hcd, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | if (retval != 0) { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2290 | usb_put_hcd(hs_hcd); |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2291 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | } |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2293 | |
| 2294 | if (mod_data.is_super_speed) { |
| 2295 | ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev, |
| 2296 | dev_name(&pdev->dev), hs_hcd); |
| 2297 | if (!ss_hcd) { |
| 2298 | retval = -ENOMEM; |
| 2299 | goto dealloc_usb2_hcd; |
| 2300 | } |
| 2301 | |
| 2302 | retval = usb_add_hcd(ss_hcd, 0, 0); |
| 2303 | if (retval) |
| 2304 | goto put_usb3_hcd; |
| 2305 | } |
| 2306 | return 0; |
| 2307 | |
| 2308 | put_usb3_hcd: |
| 2309 | usb_put_hcd(ss_hcd); |
| 2310 | dealloc_usb2_hcd: |
| 2311 | usb_put_hcd(hs_hcd); |
| 2312 | the_controller.hs_hcd = the_controller.ss_hcd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | return retval; |
| 2314 | } |
| 2315 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2316 | static int dummy_hcd_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | { |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2318 | struct dummy *dum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2320 | dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2321 | |
| 2322 | if (dum->ss_hcd) { |
| 2323 | usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd)); |
| 2324 | usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd)); |
| 2325 | } |
| 2326 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2327 | usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd)); |
| 2328 | usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd)); |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2329 | |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2330 | the_controller.hs_hcd = NULL; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2331 | the_controller.ss_hcd = NULL; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2332 | |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2333 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | } |
| 2335 | |
Alan Stern | 8364d6b | 2005-11-14 12:16:30 -0500 | [diff] [blame] | 2336 | static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2337 | { |
| 2338 | struct usb_hcd *hcd; |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2339 | struct dummy_hcd *dum_hcd; |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2340 | int rc = 0; |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2341 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2342 | dev_dbg (&pdev->dev, "%s\n", __func__); |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2343 | |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2344 | hcd = platform_get_drvdata (pdev); |
Tatyana Brokhman | cdfcbd2 | 2011-06-29 16:41:51 +0300 | [diff] [blame] | 2345 | dum_hcd = hcd_to_dummy_hcd(hcd); |
| 2346 | if (dum_hcd->rh_state == DUMMY_RH_RUNNING) { |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2347 | dev_warn(&pdev->dev, "Root hub isn't suspended!\n"); |
| 2348 | rc = -EBUSY; |
| 2349 | } else |
| 2350 | clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
| 2351 | return rc; |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2352 | } |
| 2353 | |
Alan Stern | 8364d6b | 2005-11-14 12:16:30 -0500 | [diff] [blame] | 2354 | static int dummy_hcd_resume (struct platform_device *pdev) |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2355 | { |
| 2356 | struct usb_hcd *hcd; |
| 2357 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2358 | dev_dbg (&pdev->dev, "%s\n", __func__); |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2359 | |
Alan Stern | 3cf0a22 | 2005-11-29 12:08:15 -0500 | [diff] [blame] | 2360 | hcd = platform_get_drvdata (pdev); |
| 2361 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2362 | usb_hcd_poll_rh_status (hcd); |
| 2363 | return 0; |
| 2364 | } |
| 2365 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2366 | static struct platform_driver dummy_hcd_driver = { |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2367 | .probe = dummy_hcd_probe, |
| 2368 | .remove = dummy_hcd_remove, |
Alan Stern | 391eca9 | 2005-05-10 15:34:16 -0400 | [diff] [blame] | 2369 | .suspend = dummy_hcd_suspend, |
| 2370 | .resume = dummy_hcd_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2371 | .driver = { |
| 2372 | .name = (char *) driver_name, |
| 2373 | .owner = THIS_MODULE, |
| 2374 | }, |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2375 | }; |
| 2376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | /*-------------------------------------------------------------------------*/ |
| 2378 | |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2379 | static struct platform_device *the_udc_pdev; |
| 2380 | static struct platform_device *the_hcd_pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | |
| 2382 | static int __init init (void) |
| 2383 | { |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2384 | int retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | |
| 2386 | if (usb_disabled ()) |
| 2387 | return -ENODEV; |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2388 | |
Tatyana Brokhman | 7eca4c5 | 2011-06-29 16:41:53 +0300 | [diff] [blame] | 2389 | if (!mod_data.is_high_speed && mod_data.is_super_speed) |
| 2390 | return -EINVAL; |
| 2391 | |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2392 | the_hcd_pdev = platform_device_alloc(driver_name, -1); |
| 2393 | if (!the_hcd_pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | return retval; |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2395 | the_udc_pdev = platform_device_alloc(gadget_name, -1); |
| 2396 | if (!the_udc_pdev) |
| 2397 | goto err_alloc_udc; |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2398 | |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2399 | retval = platform_driver_register(&dummy_hcd_driver); |
| 2400 | if (retval < 0) |
| 2401 | goto err_register_hcd_driver; |
| 2402 | retval = platform_driver_register(&dummy_udc_driver); |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2403 | if (retval < 0) |
| 2404 | goto err_register_udc_driver; |
| 2405 | |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2406 | retval = platform_device_add(the_hcd_pdev); |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2407 | if (retval < 0) |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2408 | goto err_add_hcd; |
Tatyana Brokhman | 1cd8fd2 | 2011-06-29 16:41:52 +0300 | [diff] [blame] | 2409 | if (!the_controller.hs_hcd || |
| 2410 | (!the_controller.ss_hcd && mod_data.is_super_speed)) { |
Sebastian Andrzej Siewior | 865835f | 2011-04-15 20:37:06 +0200 | [diff] [blame] | 2411 | /* |
| 2412 | * The hcd was added successfully but its probe function failed |
| 2413 | * for some reason. |
| 2414 | */ |
| 2415 | retval = -EINVAL; |
| 2416 | goto err_add_udc; |
| 2417 | } |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2418 | retval = platform_device_add(the_udc_pdev); |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2419 | if (retval < 0) |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2420 | goto err_add_udc; |
Sebastian Andrzej Siewior | 865835f | 2011-04-15 20:37:06 +0200 | [diff] [blame] | 2421 | if (!platform_get_drvdata(the_udc_pdev)) { |
| 2422 | /* |
| 2423 | * The udc was added successfully but its probe function failed |
| 2424 | * for some reason. |
| 2425 | */ |
| 2426 | retval = -EINVAL; |
| 2427 | goto err_probe_udc; |
| 2428 | } |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2429 | return retval; |
| 2430 | |
Sebastian Andrzej Siewior | 865835f | 2011-04-15 20:37:06 +0200 | [diff] [blame] | 2431 | err_probe_udc: |
| 2432 | platform_device_del(the_udc_pdev); |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2433 | err_add_udc: |
| 2434 | platform_device_del(the_hcd_pdev); |
| 2435 | err_add_hcd: |
| 2436 | platform_driver_unregister(&dummy_udc_driver); |
Alan Stern | d9b7625 | 2005-05-03 16:15:43 -0400 | [diff] [blame] | 2437 | err_register_udc_driver: |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2438 | platform_driver_unregister(&dummy_hcd_driver); |
| 2439 | err_register_hcd_driver: |
| 2440 | platform_device_put(the_udc_pdev); |
| 2441 | err_alloc_udc: |
| 2442 | platform_device_put(the_hcd_pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | return retval; |
| 2444 | } |
| 2445 | module_init (init); |
| 2446 | |
| 2447 | static void __exit cleanup (void) |
| 2448 | { |
Alan Stern | a89a2cd | 2008-04-07 15:03:25 -0400 | [diff] [blame] | 2449 | platform_device_unregister(the_udc_pdev); |
| 2450 | platform_device_unregister(the_hcd_pdev); |
| 2451 | platform_driver_unregister(&dummy_udc_driver); |
| 2452 | platform_driver_unregister(&dummy_hcd_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | } |
| 2454 | module_exit (cleanup); |