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