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