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