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