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