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