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