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