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