David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * f_sourcesink.c - USB peripheral source/sink configuration driver |
| 3 | * |
| 4 | * Copyright (C) 2003-2008 David Brownell |
| 5 | * Copyright (C) 2008 by Nokia Corporation |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* #define VERBOSE_DEBUG */ |
| 14 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 17 | #include <linux/device.h> |
Paul Gortmaker | 6eb0de8 | 2011-07-03 16:09:31 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 19 | #include <linux/usb/composite.h> |
| 20 | #include <linux/err.h> |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 21 | |
| 22 | #include "g_zero.h" |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 23 | #include "u_f.h" |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 24 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 25 | /* |
| 26 | * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral |
| 27 | * controller drivers. |
| 28 | * |
| 29 | * This just sinks bulk packets OUT to the peripheral and sources them IN |
| 30 | * to the host, optionally with specific data patterns for integrity tests. |
| 31 | * As such it supports basic functionality and load tests. |
| 32 | * |
| 33 | * In terms of control messaging, this supports all the standard requests |
| 34 | * plus two that support control-OUT tests. If the optional "autoresume" |
| 35 | * mode is enabled, it provides good functional coverage for the "USBCV" |
| 36 | * test harness from USB-IF. |
| 37 | * |
| 38 | * Note that because this doesn't queue more than one request at a time, |
| 39 | * some other function must be used to test queueing logic. The network |
| 40 | * link (g_ether) is the best overall option for that, since its TX and RX |
| 41 | * queues are relatively independent, will receive a range of packet sizes, |
| 42 | * and can often be made to run out completely. Those issues are important |
| 43 | * when stress testing peripheral controller drivers. |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 44 | */ |
| 45 | struct f_sourcesink { |
| 46 | struct usb_function function; |
| 47 | |
| 48 | struct usb_ep *in_ep; |
| 49 | struct usb_ep *out_ep; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 50 | struct usb_ep *iso_in_ep; |
| 51 | struct usb_ep *iso_out_ep; |
| 52 | int cur_alt; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static inline struct f_sourcesink *func_to_ss(struct usb_function *f) |
| 56 | { |
| 57 | return container_of(f, struct f_sourcesink, function); |
| 58 | } |
| 59 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 60 | static unsigned pattern; |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 61 | static unsigned isoc_interval; |
| 62 | static unsigned isoc_maxpacket; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 63 | static unsigned isoc_mult; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 64 | static unsigned isoc_maxburst; |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 65 | static unsigned buflen; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 66 | |
| 67 | /*-------------------------------------------------------------------------*/ |
| 68 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 69 | static struct usb_interface_descriptor source_sink_intf_alt0 = { |
| 70 | .bLength = USB_DT_INTERFACE_SIZE, |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 71 | .bDescriptorType = USB_DT_INTERFACE, |
| 72 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 73 | .bAlternateSetting = 0, |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 74 | .bNumEndpoints = 2, |
| 75 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 76 | /* .iInterface = DYNAMIC */ |
| 77 | }; |
| 78 | |
| 79 | static struct usb_interface_descriptor source_sink_intf_alt1 = { |
| 80 | .bLength = USB_DT_INTERFACE_SIZE, |
| 81 | .bDescriptorType = USB_DT_INTERFACE, |
| 82 | |
| 83 | .bAlternateSetting = 1, |
| 84 | .bNumEndpoints = 4, |
| 85 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, |
| 86 | /* .iInterface = DYNAMIC */ |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /* full speed support: */ |
| 90 | |
| 91 | static struct usb_endpoint_descriptor fs_source_desc = { |
| 92 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 93 | .bDescriptorType = USB_DT_ENDPOINT, |
| 94 | |
| 95 | .bEndpointAddress = USB_DIR_IN, |
| 96 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 97 | }; |
| 98 | |
| 99 | static struct usb_endpoint_descriptor fs_sink_desc = { |
| 100 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 101 | .bDescriptorType = USB_DT_ENDPOINT, |
| 102 | |
| 103 | .bEndpointAddress = USB_DIR_OUT, |
| 104 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 105 | }; |
| 106 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 107 | static struct usb_endpoint_descriptor fs_iso_source_desc = { |
| 108 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 109 | .bDescriptorType = USB_DT_ENDPOINT, |
| 110 | |
| 111 | .bEndpointAddress = USB_DIR_IN, |
| 112 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
| 113 | .wMaxPacketSize = cpu_to_le16(1023), |
| 114 | .bInterval = 4, |
| 115 | }; |
| 116 | |
| 117 | static struct usb_endpoint_descriptor fs_iso_sink_desc = { |
| 118 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 119 | .bDescriptorType = USB_DT_ENDPOINT, |
| 120 | |
| 121 | .bEndpointAddress = USB_DIR_OUT, |
| 122 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
| 123 | .wMaxPacketSize = cpu_to_le16(1023), |
| 124 | .bInterval = 4, |
| 125 | }; |
| 126 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 127 | static struct usb_descriptor_header *fs_source_sink_descs[] = { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 128 | (struct usb_descriptor_header *) &source_sink_intf_alt0, |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 129 | (struct usb_descriptor_header *) &fs_sink_desc, |
| 130 | (struct usb_descriptor_header *) &fs_source_desc, |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 131 | (struct usb_descriptor_header *) &source_sink_intf_alt1, |
| 132 | #define FS_ALT_IFC_1_OFFSET 3 |
| 133 | (struct usb_descriptor_header *) &fs_sink_desc, |
| 134 | (struct usb_descriptor_header *) &fs_source_desc, |
| 135 | (struct usb_descriptor_header *) &fs_iso_sink_desc, |
| 136 | (struct usb_descriptor_header *) &fs_iso_source_desc, |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 137 | NULL, |
| 138 | }; |
| 139 | |
| 140 | /* high speed support: */ |
| 141 | |
| 142 | static struct usb_endpoint_descriptor hs_source_desc = { |
| 143 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 144 | .bDescriptorType = USB_DT_ENDPOINT, |
| 145 | |
| 146 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 147 | .wMaxPacketSize = cpu_to_le16(512), |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static struct usb_endpoint_descriptor hs_sink_desc = { |
| 151 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 152 | .bDescriptorType = USB_DT_ENDPOINT, |
| 153 | |
| 154 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 155 | .wMaxPacketSize = cpu_to_le16(512), |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 158 | static struct usb_endpoint_descriptor hs_iso_source_desc = { |
| 159 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 160 | .bDescriptorType = USB_DT_ENDPOINT, |
| 161 | |
| 162 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
| 163 | .wMaxPacketSize = cpu_to_le16(1024), |
| 164 | .bInterval = 4, |
| 165 | }; |
| 166 | |
| 167 | static struct usb_endpoint_descriptor hs_iso_sink_desc = { |
| 168 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 169 | .bDescriptorType = USB_DT_ENDPOINT, |
| 170 | |
| 171 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
| 172 | .wMaxPacketSize = cpu_to_le16(1024), |
| 173 | .bInterval = 4, |
| 174 | }; |
| 175 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 176 | static struct usb_descriptor_header *hs_source_sink_descs[] = { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 177 | (struct usb_descriptor_header *) &source_sink_intf_alt0, |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 178 | (struct usb_descriptor_header *) &hs_source_desc, |
| 179 | (struct usb_descriptor_header *) &hs_sink_desc, |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 180 | (struct usb_descriptor_header *) &source_sink_intf_alt1, |
| 181 | #define HS_ALT_IFC_1_OFFSET 3 |
| 182 | (struct usb_descriptor_header *) &hs_source_desc, |
| 183 | (struct usb_descriptor_header *) &hs_sink_desc, |
| 184 | (struct usb_descriptor_header *) &hs_iso_source_desc, |
| 185 | (struct usb_descriptor_header *) &hs_iso_sink_desc, |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 186 | NULL, |
| 187 | }; |
| 188 | |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 189 | /* super speed support: */ |
| 190 | |
| 191 | static struct usb_endpoint_descriptor ss_source_desc = { |
| 192 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 193 | .bDescriptorType = USB_DT_ENDPOINT, |
| 194 | |
| 195 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 196 | .wMaxPacketSize = cpu_to_le16(1024), |
| 197 | }; |
| 198 | |
Jingoo Han | 4a5ee77 | 2013-12-16 18:44:43 +0900 | [diff] [blame] | 199 | static struct usb_ss_ep_comp_descriptor ss_source_comp_desc = { |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 200 | .bLength = USB_DT_SS_EP_COMP_SIZE, |
| 201 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 202 | |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 203 | .bMaxBurst = 0, |
| 204 | .bmAttributes = 0, |
| 205 | .wBytesPerInterval = 0, |
| 206 | }; |
| 207 | |
| 208 | static struct usb_endpoint_descriptor ss_sink_desc = { |
| 209 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 210 | .bDescriptorType = USB_DT_ENDPOINT, |
| 211 | |
| 212 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 213 | .wMaxPacketSize = cpu_to_le16(1024), |
| 214 | }; |
| 215 | |
Jingoo Han | 4a5ee77 | 2013-12-16 18:44:43 +0900 | [diff] [blame] | 216 | static struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = { |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 217 | .bLength = USB_DT_SS_EP_COMP_SIZE, |
| 218 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 219 | |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 220 | .bMaxBurst = 0, |
| 221 | .bmAttributes = 0, |
| 222 | .wBytesPerInterval = 0, |
| 223 | }; |
| 224 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 225 | static struct usb_endpoint_descriptor ss_iso_source_desc = { |
| 226 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 227 | .bDescriptorType = USB_DT_ENDPOINT, |
| 228 | |
| 229 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
| 230 | .wMaxPacketSize = cpu_to_le16(1024), |
| 231 | .bInterval = 4, |
| 232 | }; |
| 233 | |
Jingoo Han | 4a5ee77 | 2013-12-16 18:44:43 +0900 | [diff] [blame] | 234 | static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 235 | .bLength = USB_DT_SS_EP_COMP_SIZE, |
| 236 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 237 | |
| 238 | .bMaxBurst = 0, |
| 239 | .bmAttributes = 0, |
| 240 | .wBytesPerInterval = cpu_to_le16(1024), |
| 241 | }; |
| 242 | |
| 243 | static struct usb_endpoint_descriptor ss_iso_sink_desc = { |
| 244 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 245 | .bDescriptorType = USB_DT_ENDPOINT, |
| 246 | |
| 247 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
| 248 | .wMaxPacketSize = cpu_to_le16(1024), |
| 249 | .bInterval = 4, |
| 250 | }; |
| 251 | |
Jingoo Han | 4a5ee77 | 2013-12-16 18:44:43 +0900 | [diff] [blame] | 252 | static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 253 | .bLength = USB_DT_SS_EP_COMP_SIZE, |
| 254 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 255 | |
| 256 | .bMaxBurst = 0, |
| 257 | .bmAttributes = 0, |
| 258 | .wBytesPerInterval = cpu_to_le16(1024), |
| 259 | }; |
| 260 | |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 261 | static struct usb_descriptor_header *ss_source_sink_descs[] = { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 262 | (struct usb_descriptor_header *) &source_sink_intf_alt0, |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 263 | (struct usb_descriptor_header *) &ss_source_desc, |
| 264 | (struct usb_descriptor_header *) &ss_source_comp_desc, |
| 265 | (struct usb_descriptor_header *) &ss_sink_desc, |
| 266 | (struct usb_descriptor_header *) &ss_sink_comp_desc, |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 267 | (struct usb_descriptor_header *) &source_sink_intf_alt1, |
| 268 | #define SS_ALT_IFC_1_OFFSET 5 |
| 269 | (struct usb_descriptor_header *) &ss_source_desc, |
| 270 | (struct usb_descriptor_header *) &ss_source_comp_desc, |
| 271 | (struct usb_descriptor_header *) &ss_sink_desc, |
| 272 | (struct usb_descriptor_header *) &ss_sink_comp_desc, |
| 273 | (struct usb_descriptor_header *) &ss_iso_source_desc, |
| 274 | (struct usb_descriptor_header *) &ss_iso_source_comp_desc, |
| 275 | (struct usb_descriptor_header *) &ss_iso_sink_desc, |
| 276 | (struct usb_descriptor_header *) &ss_iso_sink_comp_desc, |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 277 | NULL, |
| 278 | }; |
| 279 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 280 | /* function-specific strings: */ |
| 281 | |
| 282 | static struct usb_string strings_sourcesink[] = { |
| 283 | [0].s = "source and sink data", |
| 284 | { } /* end of list */ |
| 285 | }; |
| 286 | |
| 287 | static struct usb_gadget_strings stringtab_sourcesink = { |
| 288 | .language = 0x0409, /* en-us */ |
| 289 | .strings = strings_sourcesink, |
| 290 | }; |
| 291 | |
| 292 | static struct usb_gadget_strings *sourcesink_strings[] = { |
| 293 | &stringtab_sourcesink, |
| 294 | NULL, |
| 295 | }; |
| 296 | |
| 297 | /*-------------------------------------------------------------------------*/ |
| 298 | |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 299 | static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len) |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 300 | { |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 301 | return alloc_ep_req(ep, len, buflen); |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void free_ep_req(struct usb_ep *ep, struct usb_request *req) |
| 305 | { |
| 306 | kfree(req->buf); |
| 307 | usb_ep_free_request(ep, req); |
| 308 | } |
| 309 | |
| 310 | static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep) |
| 311 | { |
| 312 | int value; |
| 313 | |
| 314 | if (ep->driver_data) { |
| 315 | value = usb_ep_disable(ep); |
| 316 | if (value < 0) |
| 317 | DBG(cdev, "disable %s --> %d\n", |
| 318 | ep->name, value); |
| 319 | ep->driver_data = NULL; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void disable_endpoints(struct usb_composite_dev *cdev, |
| 324 | struct usb_ep *in, struct usb_ep *out, |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 325 | struct usb_ep *iso_in, struct usb_ep *iso_out) |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 326 | { |
| 327 | disable_ep(cdev, in); |
| 328 | disable_ep(cdev, out); |
| 329 | if (iso_in) |
| 330 | disable_ep(cdev, iso_in); |
| 331 | if (iso_out) |
| 332 | disable_ep(cdev, iso_out); |
| 333 | } |
| 334 | |
| 335 | static int |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 336 | sourcesink_bind(struct usb_configuration *c, struct usb_function *f) |
| 337 | { |
| 338 | struct usb_composite_dev *cdev = c->cdev; |
| 339 | struct f_sourcesink *ss = func_to_ss(f); |
| 340 | int id; |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 341 | int ret; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 342 | |
| 343 | /* allocate interface ID(s) */ |
| 344 | id = usb_interface_id(c, f); |
| 345 | if (id < 0) |
| 346 | return id; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 347 | source_sink_intf_alt0.bInterfaceNumber = id; |
| 348 | source_sink_intf_alt1.bInterfaceNumber = id; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 349 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 350 | /* allocate bulk endpoints */ |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 351 | ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc); |
| 352 | if (!ss->in_ep) { |
| 353 | autoconf_fail: |
| 354 | ERROR(cdev, "%s: can't autoconfigure on %s\n", |
| 355 | f->name, cdev->gadget->name); |
| 356 | return -ENODEV; |
| 357 | } |
| 358 | ss->in_ep->driver_data = cdev; /* claim */ |
| 359 | |
| 360 | ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc); |
| 361 | if (!ss->out_ep) |
| 362 | goto autoconf_fail; |
| 363 | ss->out_ep->driver_data = cdev; /* claim */ |
| 364 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 365 | /* sanity check the isoc module parameters */ |
| 366 | if (isoc_interval < 1) |
| 367 | isoc_interval = 1; |
| 368 | if (isoc_interval > 16) |
| 369 | isoc_interval = 16; |
| 370 | if (isoc_mult > 2) |
| 371 | isoc_mult = 2; |
| 372 | if (isoc_maxburst > 15) |
| 373 | isoc_maxburst = 15; |
| 374 | |
| 375 | /* fill in the FS isoc descriptors from the module parameters */ |
| 376 | fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ? |
| 377 | 1023 : isoc_maxpacket; |
| 378 | fs_iso_source_desc.bInterval = isoc_interval; |
| 379 | fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ? |
| 380 | 1023 : isoc_maxpacket; |
| 381 | fs_iso_sink_desc.bInterval = isoc_interval; |
| 382 | |
| 383 | /* allocate iso endpoints */ |
| 384 | ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc); |
| 385 | if (!ss->iso_in_ep) |
| 386 | goto no_iso; |
| 387 | ss->iso_in_ep->driver_data = cdev; /* claim */ |
| 388 | |
| 389 | ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc); |
| 390 | if (ss->iso_out_ep) { |
| 391 | ss->iso_out_ep->driver_data = cdev; /* claim */ |
| 392 | } else { |
| 393 | ss->iso_in_ep->driver_data = NULL; |
| 394 | ss->iso_in_ep = NULL; |
| 395 | no_iso: |
| 396 | /* |
| 397 | * We still want to work even if the UDC doesn't have isoc |
| 398 | * endpoints, so null out the alt interface that contains |
| 399 | * them and continue. |
| 400 | */ |
| 401 | fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL; |
| 402 | hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL; |
| 403 | ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL; |
| 404 | } |
| 405 | |
| 406 | if (isoc_maxpacket > 1024) |
| 407 | isoc_maxpacket = 1024; |
| 408 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 409 | /* support high speed hardware */ |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 410 | hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress; |
| 411 | hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 412 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 413 | /* |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 414 | * Fill in the HS isoc descriptors from the module parameters. |
| 415 | * We assume that the user knows what they are doing and won't |
| 416 | * give parameters that their UDC doesn't support. |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 417 | */ |
| 418 | hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket; |
| 419 | hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11; |
| 420 | hs_iso_source_desc.bInterval = isoc_interval; |
| 421 | hs_iso_source_desc.bEndpointAddress = |
| 422 | fs_iso_source_desc.bEndpointAddress; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 423 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 424 | hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket; |
| 425 | hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11; |
| 426 | hs_iso_sink_desc.bInterval = isoc_interval; |
| 427 | hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 428 | |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 429 | /* support super speed hardware */ |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 430 | ss_source_desc.bEndpointAddress = |
| 431 | fs_source_desc.bEndpointAddress; |
| 432 | ss_sink_desc.bEndpointAddress = |
| 433 | fs_sink_desc.bEndpointAddress; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 434 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 435 | /* |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 436 | * Fill in the SS isoc descriptors from the module parameters. |
| 437 | * We assume that the user knows what they are doing and won't |
| 438 | * give parameters that their UDC doesn't support. |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 439 | */ |
| 440 | ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket; |
| 441 | ss_iso_source_desc.bInterval = isoc_interval; |
| 442 | ss_iso_source_comp_desc.bmAttributes = isoc_mult; |
| 443 | ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst; |
| 444 | ss_iso_source_comp_desc.wBytesPerInterval = |
| 445 | isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1); |
| 446 | ss_iso_source_desc.bEndpointAddress = |
| 447 | fs_iso_source_desc.bEndpointAddress; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 448 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 449 | ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket; |
| 450 | ss_iso_sink_desc.bInterval = isoc_interval; |
| 451 | ss_iso_sink_comp_desc.bmAttributes = isoc_mult; |
| 452 | ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst; |
| 453 | ss_iso_sink_comp_desc.wBytesPerInterval = |
| 454 | isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1); |
| 455 | ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 456 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 457 | ret = usb_assign_descriptors(f, fs_source_sink_descs, |
| 458 | hs_source_sink_descs, ss_source_sink_descs); |
| 459 | if (ret) |
| 460 | return ret; |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 461 | |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 462 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n", |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 463 | (gadget_is_superspeed(c->cdev->gadget) ? "super" : |
| 464 | (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")), |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 465 | f->name, ss->in_ep->name, ss->out_ep->name, |
| 466 | ss->iso_in_ep ? ss->iso_in_ep->name : "<none>", |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 467 | ss->iso_out_ep ? ss->iso_out_ep->name : "<none>"); |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static void |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 472 | sourcesink_free_func(struct usb_function *f) |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 473 | { |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 474 | struct f_ss_opts *opts; |
| 475 | |
| 476 | opts = container_of(f->fi, struct f_ss_opts, func_inst); |
| 477 | |
| 478 | mutex_lock(&opts->lock); |
| 479 | opts->refcnt--; |
| 480 | mutex_unlock(&opts->lock); |
| 481 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 482 | usb_free_all_descriptors(f); |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 483 | kfree(func_to_ss(f)); |
| 484 | } |
| 485 | |
| 486 | /* optionally require specific source/sink data patterns */ |
| 487 | static int check_read_data(struct f_sourcesink *ss, struct usb_request *req) |
| 488 | { |
| 489 | unsigned i; |
| 490 | u8 *buf = req->buf; |
| 491 | struct usb_composite_dev *cdev = ss->function.config->cdev; |
| 492 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 493 | if (pattern == 2) |
| 494 | return 0; |
| 495 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 496 | for (i = 0; i < req->actual; i++, buf++) { |
| 497 | switch (pattern) { |
| 498 | |
| 499 | /* all-zeroes has no synchronization issues */ |
| 500 | case 0: |
| 501 | if (*buf == 0) |
| 502 | continue; |
| 503 | break; |
| 504 | |
| 505 | /* "mod63" stays in sync with short-terminated transfers, |
| 506 | * OR otherwise when host and gadget agree on how large |
| 507 | * each usb transfer request should be. Resync is done |
| 508 | * with set_interface or set_config. (We *WANT* it to |
| 509 | * get quickly out of sync if controllers or their drivers |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 510 | * stutter for any reason, including buffer duplication...) |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 511 | */ |
| 512 | case 1: |
| 513 | if (*buf == (u8)(i % 63)) |
| 514 | continue; |
| 515 | break; |
| 516 | } |
| 517 | ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf); |
| 518 | usb_ep_set_halt(ss->out_ep); |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | static void reinit_write_data(struct usb_ep *ep, struct usb_request *req) |
| 525 | { |
| 526 | unsigned i; |
| 527 | u8 *buf = req->buf; |
| 528 | |
| 529 | switch (pattern) { |
| 530 | case 0: |
| 531 | memset(req->buf, 0, req->length); |
| 532 | break; |
| 533 | case 1: |
| 534 | for (i = 0; i < req->length; i++) |
| 535 | *buf++ = (u8) (i % 63); |
| 536 | break; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 537 | case 2: |
| 538 | break; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | static void source_sink_complete(struct usb_ep *ep, struct usb_request *req) |
| 543 | { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 544 | struct usb_composite_dev *cdev; |
| 545 | struct f_sourcesink *ss = ep->driver_data; |
| 546 | int status = req->status; |
| 547 | |
| 548 | /* driver_data will be null if ep has been disabled */ |
| 549 | if (!ss) |
| 550 | return; |
| 551 | |
| 552 | cdev = ss->function.config->cdev; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 553 | |
| 554 | switch (status) { |
| 555 | |
| 556 | case 0: /* normal completion? */ |
| 557 | if (ep == ss->out_ep) { |
| 558 | check_read_data(ss, req); |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 559 | if (pattern != 2) |
| 560 | memset(req->buf, 0x55, req->length); |
Armando Visconti | 32c9cf2 | 2012-12-13 15:11:19 +0100 | [diff] [blame] | 561 | } |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 562 | break; |
| 563 | |
| 564 | /* this endpoint is normally active while we're configured */ |
| 565 | case -ECONNABORTED: /* hardware forced ep reset */ |
| 566 | case -ECONNRESET: /* request dequeued */ |
| 567 | case -ESHUTDOWN: /* disconnect from host */ |
| 568 | VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status, |
| 569 | req->actual, req->length); |
| 570 | if (ep == ss->out_ep) |
| 571 | check_read_data(ss, req); |
| 572 | free_ep_req(ep, req); |
| 573 | return; |
| 574 | |
| 575 | case -EOVERFLOW: /* buffer overrun on read means that |
| 576 | * we didn't provide a big enough |
| 577 | * buffer. |
| 578 | */ |
| 579 | default: |
| 580 | #if 1 |
| 581 | DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name, |
| 582 | status, req->actual, req->length); |
| 583 | #endif |
| 584 | case -EREMOTEIO: /* short read */ |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | status = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 589 | if (status) { |
| 590 | ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n", |
| 591 | ep->name, req->length, status); |
| 592 | usb_ep_set_halt(ep); |
| 593 | /* FIXME recover later ... somehow */ |
| 594 | } |
| 595 | } |
| 596 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 597 | static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in, |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 598 | bool is_iso, int speed) |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 599 | { |
| 600 | struct usb_ep *ep; |
| 601 | struct usb_request *req; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 602 | int i, size, status; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 603 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 604 | for (i = 0; i < 8; i++) { |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 605 | if (is_iso) { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 606 | switch (speed) { |
| 607 | case USB_SPEED_SUPER: |
| 608 | size = isoc_maxpacket * (isoc_mult + 1) * |
| 609 | (isoc_maxburst + 1); |
| 610 | break; |
| 611 | case USB_SPEED_HIGH: |
| 612 | size = isoc_maxpacket * (isoc_mult + 1); |
| 613 | break; |
| 614 | default: |
| 615 | size = isoc_maxpacket > 1023 ? |
| 616 | 1023 : isoc_maxpacket; |
| 617 | break; |
| 618 | } |
| 619 | ep = is_in ? ss->iso_in_ep : ss->iso_out_ep; |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 620 | req = ss_alloc_ep_req(ep, size); |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 621 | } else { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 622 | ep = is_in ? ss->in_ep : ss->out_ep; |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 623 | req = ss_alloc_ep_req(ep, 0); |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 624 | } |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 625 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 626 | if (!req) |
| 627 | return -ENOMEM; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 628 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 629 | req->complete = source_sink_complete; |
| 630 | if (is_in) |
| 631 | reinit_write_data(ep, req); |
| 632 | else if (pattern != 2) |
| 633 | memset(req->buf, 0x55, req->length); |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 634 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 635 | status = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 636 | if (status) { |
| 637 | struct usb_composite_dev *cdev; |
| 638 | |
| 639 | cdev = ss->function.config->cdev; |
| 640 | ERROR(cdev, "start %s%s %s --> %d\n", |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 641 | is_iso ? "ISO-" : "", is_in ? "IN" : "OUT", |
| 642 | ep->name, status); |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 643 | free_ep_req(ep, req); |
| 644 | } |
| 645 | |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 646 | if (!is_iso) |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 647 | break; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | return status; |
| 651 | } |
| 652 | |
| 653 | static void disable_source_sink(struct f_sourcesink *ss) |
| 654 | { |
| 655 | struct usb_composite_dev *cdev; |
| 656 | |
| 657 | cdev = ss->function.config->cdev; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 658 | disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep, |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 659 | ss->iso_out_ep); |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 660 | VDBG(cdev, "%s disabled\n", ss->function.name); |
| 661 | } |
| 662 | |
| 663 | static int |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 664 | enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss, |
| 665 | int alt) |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 666 | { |
| 667 | int result = 0; |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 668 | int speed = cdev->gadget->speed; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 669 | struct usb_ep *ep; |
| 670 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 671 | /* one bulk endpoint writes (sources) zeroes IN (to the host) */ |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 672 | ep = ss->in_ep; |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 673 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); |
| 674 | if (result) |
| 675 | return result; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 676 | result = usb_ep_enable(ep); |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 677 | if (result < 0) |
| 678 | return result; |
| 679 | ep->driver_data = ss; |
| 680 | |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 681 | result = source_sink_start_ep(ss, true, false, speed); |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 682 | if (result < 0) { |
| 683 | fail: |
| 684 | ep = ss->in_ep; |
| 685 | usb_ep_disable(ep); |
| 686 | ep->driver_data = NULL; |
| 687 | return result; |
| 688 | } |
| 689 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 690 | /* one bulk endpoint reads (sinks) anything OUT (from the host) */ |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 691 | ep = ss->out_ep; |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 692 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); |
| 693 | if (result) |
| 694 | goto fail; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 695 | result = usb_ep_enable(ep); |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 696 | if (result < 0) |
| 697 | goto fail; |
| 698 | ep->driver_data = ss; |
| 699 | |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 700 | result = source_sink_start_ep(ss, false, false, speed); |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 701 | if (result < 0) { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 702 | fail2: |
| 703 | ep = ss->out_ep; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 704 | usb_ep_disable(ep); |
| 705 | ep->driver_data = NULL; |
| 706 | goto fail; |
| 707 | } |
| 708 | |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 709 | if (alt == 0) |
| 710 | goto out; |
| 711 | |
| 712 | /* one iso endpoint writes (sources) zeroes IN (to the host) */ |
| 713 | ep = ss->iso_in_ep; |
| 714 | if (ep) { |
| 715 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); |
| 716 | if (result) |
| 717 | goto fail2; |
| 718 | result = usb_ep_enable(ep); |
| 719 | if (result < 0) |
| 720 | goto fail2; |
| 721 | ep->driver_data = ss; |
| 722 | |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 723 | result = source_sink_start_ep(ss, true, true, speed); |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 724 | if (result < 0) { |
| 725 | fail3: |
| 726 | ep = ss->iso_in_ep; |
| 727 | if (ep) { |
| 728 | usb_ep_disable(ep); |
| 729 | ep->driver_data = NULL; |
| 730 | } |
| 731 | goto fail2; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /* one iso endpoint reads (sinks) anything OUT (from the host) */ |
| 736 | ep = ss->iso_out_ep; |
| 737 | if (ep) { |
| 738 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); |
| 739 | if (result) |
| 740 | goto fail3; |
| 741 | result = usb_ep_enable(ep); |
| 742 | if (result < 0) |
| 743 | goto fail3; |
| 744 | ep->driver_data = ss; |
| 745 | |
Felipe Balbi | 2c24780 | 2015-03-11 10:00:05 -0500 | [diff] [blame] | 746 | result = source_sink_start_ep(ss, false, true, speed); |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 747 | if (result < 0) { |
| 748 | usb_ep_disable(ep); |
| 749 | ep->driver_data = NULL; |
| 750 | goto fail3; |
| 751 | } |
| 752 | } |
| 753 | out: |
| 754 | ss->cur_alt = alt; |
| 755 | |
| 756 | DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt); |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 757 | return result; |
| 758 | } |
| 759 | |
| 760 | static int sourcesink_set_alt(struct usb_function *f, |
| 761 | unsigned intf, unsigned alt) |
| 762 | { |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 763 | struct f_sourcesink *ss = func_to_ss(f); |
| 764 | struct usb_composite_dev *cdev = f->config->cdev; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 765 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 766 | if (ss->in_ep->driver_data) |
| 767 | disable_source_sink(ss); |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 768 | return enable_source_sink(cdev, ss, alt); |
| 769 | } |
| 770 | |
| 771 | static int sourcesink_get_alt(struct usb_function *f, unsigned intf) |
| 772 | { |
| 773 | struct f_sourcesink *ss = func_to_ss(f); |
| 774 | |
| 775 | return ss->cur_alt; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void sourcesink_disable(struct usb_function *f) |
| 779 | { |
| 780 | struct f_sourcesink *ss = func_to_ss(f); |
| 781 | |
| 782 | disable_source_sink(ss); |
| 783 | } |
| 784 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 785 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 786 | |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 787 | static int sourcesink_setup(struct usb_function *f, |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 788 | const struct usb_ctrlrequest *ctrl) |
| 789 | { |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 790 | struct usb_configuration *c = f->config; |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 791 | struct usb_request *req = c->cdev->req; |
| 792 | int value = -EOPNOTSUPP; |
| 793 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 794 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 795 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 796 | |
Sebastian Andrzej Siewior | e13f17f | 2012-09-10 15:01:51 +0200 | [diff] [blame] | 797 | req->length = USB_COMP_EP0_BUFSIZ; |
Bob Liu | 5030ec7 | 2011-06-23 17:09:49 -0400 | [diff] [blame] | 798 | |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 799 | /* composite driver infrastructure handles everything except |
| 800 | * the two control test requests. |
| 801 | */ |
| 802 | switch (ctrl->bRequest) { |
| 803 | |
| 804 | /* |
| 805 | * These are the same vendor-specific requests supported by |
| 806 | * Intel's USB 2.0 compliance test devices. We exceed that |
| 807 | * device spec by allowing multiple-packet requests. |
| 808 | * |
| 809 | * NOTE: the Control-OUT data stays in req->buf ... better |
| 810 | * would be copying it into a scratch buffer, so that other |
| 811 | * requests may safely intervene. |
| 812 | */ |
| 813 | case 0x5b: /* control WRITE test -- fill the buffer */ |
| 814 | if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR)) |
| 815 | goto unknown; |
| 816 | if (w_value || w_index) |
| 817 | break; |
| 818 | /* just read that many bytes into the buffer */ |
| 819 | if (w_length > req->length) |
| 820 | break; |
| 821 | value = w_length; |
| 822 | break; |
| 823 | case 0x5c: /* control READ test -- return the buffer */ |
| 824 | if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR)) |
| 825 | goto unknown; |
| 826 | if (w_value || w_index) |
| 827 | break; |
| 828 | /* expect those bytes are still in the buffer; send back */ |
| 829 | if (w_length > req->length) |
| 830 | break; |
| 831 | value = w_length; |
| 832 | break; |
| 833 | |
| 834 | default: |
| 835 | unknown: |
| 836 | VDBG(c->cdev, |
| 837 | "unknown control req%02x.%02x v%04x i%04x l%d\n", |
| 838 | ctrl->bRequestType, ctrl->bRequest, |
| 839 | w_value, w_index, w_length); |
| 840 | } |
| 841 | |
| 842 | /* respond with data transfer or status phase? */ |
| 843 | if (value >= 0) { |
| 844 | VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n", |
| 845 | ctrl->bRequestType, ctrl->bRequest, |
| 846 | w_value, w_index, w_length); |
| 847 | req->zero = 0; |
| 848 | req->length = value; |
| 849 | value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC); |
| 850 | if (value < 0) |
Paul Zimmerman | b4036cc | 2012-04-16 14:19:06 -0700 | [diff] [blame] | 851 | ERROR(c->cdev, "source/sink response, err %d\n", |
David Brownell | a400cad | 2008-06-19 17:55:23 -0700 | [diff] [blame] | 852 | value); |
| 853 | } |
| 854 | |
| 855 | /* device either stalls (value < 0) or reports success */ |
| 856 | return value; |
| 857 | } |
| 858 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 859 | static struct usb_function *source_sink_alloc_func( |
| 860 | struct usb_function_instance *fi) |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 861 | { |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 862 | struct f_sourcesink *ss; |
| 863 | struct f_ss_opts *ss_opts; |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 864 | |
| 865 | ss = kzalloc(sizeof(*ss), GFP_KERNEL); |
| 866 | if (!ss) |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 867 | return NULL; |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 868 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 869 | ss_opts = container_of(fi, struct f_ss_opts, func_inst); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 870 | |
| 871 | mutex_lock(&ss_opts->lock); |
| 872 | ss_opts->refcnt++; |
| 873 | mutex_unlock(&ss_opts->lock); |
| 874 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 875 | pattern = ss_opts->pattern; |
| 876 | isoc_interval = ss_opts->isoc_interval; |
| 877 | isoc_maxpacket = ss_opts->isoc_maxpacket; |
| 878 | isoc_mult = ss_opts->isoc_mult; |
| 879 | isoc_maxburst = ss_opts->isoc_maxburst; |
| 880 | buflen = ss_opts->bulk_buflen; |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 881 | |
| 882 | ss->function.name = "source/sink"; |
| 883 | ss->function.bind = sourcesink_bind; |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 884 | ss->function.set_alt = sourcesink_set_alt; |
| 885 | ss->function.get_alt = sourcesink_get_alt; |
| 886 | ss->function.disable = sourcesink_disable; |
| 887 | ss->function.setup = sourcesink_setup; |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 888 | ss->function.strings = sourcesink_strings; |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 889 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 890 | ss->function.free_func = sourcesink_free_func; |
| 891 | |
| 892 | return &ss->function; |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 893 | } |
| 894 | |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 895 | static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item) |
| 896 | { |
| 897 | return container_of(to_config_group(item), struct f_ss_opts, |
| 898 | func_inst.group); |
| 899 | } |
| 900 | |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 901 | static void ss_attr_release(struct config_item *item) |
| 902 | { |
| 903 | struct f_ss_opts *ss_opts = to_f_ss_opts(item); |
| 904 | |
| 905 | usb_put_function_instance(&ss_opts->func_inst); |
| 906 | } |
| 907 | |
| 908 | static struct configfs_item_operations ss_item_ops = { |
| 909 | .release = ss_attr_release, |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 910 | }; |
| 911 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 912 | static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page) |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 913 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 914 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 915 | int result; |
| 916 | |
| 917 | mutex_lock(&opts->lock); |
Asaf Vertz | 9fdd84d | 2015-01-21 11:06:34 +0200 | [diff] [blame] | 918 | result = sprintf(page, "%u", opts->pattern); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 919 | mutex_unlock(&opts->lock); |
| 920 | |
| 921 | return result; |
| 922 | } |
| 923 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 924 | static ssize_t f_ss_opts_pattern_store(struct config_item *item, |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 925 | const char *page, size_t len) |
| 926 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 927 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 928 | int ret; |
| 929 | u8 num; |
| 930 | |
| 931 | mutex_lock(&opts->lock); |
| 932 | if (opts->refcnt) { |
| 933 | ret = -EBUSY; |
| 934 | goto end; |
| 935 | } |
| 936 | |
| 937 | ret = kstrtou8(page, 0, &num); |
| 938 | if (ret) |
| 939 | goto end; |
| 940 | |
| 941 | if (num != 0 && num != 1 && num != 2) { |
| 942 | ret = -EINVAL; |
| 943 | goto end; |
| 944 | } |
| 945 | |
| 946 | opts->pattern = num; |
| 947 | ret = len; |
| 948 | end: |
| 949 | mutex_unlock(&opts->lock); |
| 950 | return ret; |
| 951 | } |
| 952 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 953 | CONFIGFS_ATTR(f_ss_opts_, pattern); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 954 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 955 | static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page) |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 956 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 957 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 958 | int result; |
| 959 | |
| 960 | mutex_lock(&opts->lock); |
Asaf Vertz | 9fdd84d | 2015-01-21 11:06:34 +0200 | [diff] [blame] | 961 | result = sprintf(page, "%u", opts->isoc_interval); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 962 | mutex_unlock(&opts->lock); |
| 963 | |
| 964 | return result; |
| 965 | } |
| 966 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 967 | static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item, |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 968 | const char *page, size_t len) |
| 969 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 970 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 971 | int ret; |
| 972 | u8 num; |
| 973 | |
| 974 | mutex_lock(&opts->lock); |
| 975 | if (opts->refcnt) { |
| 976 | ret = -EBUSY; |
| 977 | goto end; |
| 978 | } |
| 979 | |
| 980 | ret = kstrtou8(page, 0, &num); |
| 981 | if (ret) |
| 982 | goto end; |
| 983 | |
| 984 | if (num > 16) { |
| 985 | ret = -EINVAL; |
| 986 | goto end; |
| 987 | } |
| 988 | |
| 989 | opts->isoc_interval = num; |
| 990 | ret = len; |
| 991 | end: |
| 992 | mutex_unlock(&opts->lock); |
| 993 | return ret; |
| 994 | } |
| 995 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 996 | CONFIGFS_ATTR(f_ss_opts_, isoc_interval); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 997 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 998 | static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page) |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 999 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1000 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1001 | int result; |
| 1002 | |
| 1003 | mutex_lock(&opts->lock); |
Asaf Vertz | 9fdd84d | 2015-01-21 11:06:34 +0200 | [diff] [blame] | 1004 | result = sprintf(page, "%u", opts->isoc_maxpacket); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1005 | mutex_unlock(&opts->lock); |
| 1006 | |
| 1007 | return result; |
| 1008 | } |
| 1009 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1010 | static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item, |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1011 | const char *page, size_t len) |
| 1012 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1013 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1014 | int ret; |
| 1015 | u16 num; |
| 1016 | |
| 1017 | mutex_lock(&opts->lock); |
| 1018 | if (opts->refcnt) { |
| 1019 | ret = -EBUSY; |
| 1020 | goto end; |
| 1021 | } |
| 1022 | |
| 1023 | ret = kstrtou16(page, 0, &num); |
| 1024 | if (ret) |
| 1025 | goto end; |
| 1026 | |
| 1027 | if (num > 1024) { |
| 1028 | ret = -EINVAL; |
| 1029 | goto end; |
| 1030 | } |
| 1031 | |
| 1032 | opts->isoc_maxpacket = num; |
| 1033 | ret = len; |
| 1034 | end: |
| 1035 | mutex_unlock(&opts->lock); |
| 1036 | return ret; |
| 1037 | } |
| 1038 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1039 | CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1040 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1041 | static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page) |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1042 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1043 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1044 | int result; |
| 1045 | |
| 1046 | mutex_lock(&opts->lock); |
Asaf Vertz | 9fdd84d | 2015-01-21 11:06:34 +0200 | [diff] [blame] | 1047 | result = sprintf(page, "%u", opts->isoc_mult); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1048 | mutex_unlock(&opts->lock); |
| 1049 | |
| 1050 | return result; |
| 1051 | } |
| 1052 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1053 | static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item, |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1054 | const char *page, size_t len) |
| 1055 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1056 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1057 | int ret; |
| 1058 | u8 num; |
| 1059 | |
| 1060 | mutex_lock(&opts->lock); |
| 1061 | if (opts->refcnt) { |
| 1062 | ret = -EBUSY; |
| 1063 | goto end; |
| 1064 | } |
| 1065 | |
| 1066 | ret = kstrtou8(page, 0, &num); |
| 1067 | if (ret) |
| 1068 | goto end; |
| 1069 | |
| 1070 | if (num > 2) { |
| 1071 | ret = -EINVAL; |
| 1072 | goto end; |
| 1073 | } |
| 1074 | |
| 1075 | opts->isoc_mult = num; |
| 1076 | ret = len; |
| 1077 | end: |
| 1078 | mutex_unlock(&opts->lock); |
| 1079 | return ret; |
| 1080 | } |
| 1081 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1082 | CONFIGFS_ATTR(f_ss_opts_, isoc_mult); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1083 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1084 | static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page) |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1085 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1086 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1087 | int result; |
| 1088 | |
| 1089 | mutex_lock(&opts->lock); |
Asaf Vertz | 9fdd84d | 2015-01-21 11:06:34 +0200 | [diff] [blame] | 1090 | result = sprintf(page, "%u", opts->isoc_maxburst); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1091 | mutex_unlock(&opts->lock); |
| 1092 | |
| 1093 | return result; |
| 1094 | } |
| 1095 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1096 | static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item, |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1097 | const char *page, size_t len) |
| 1098 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1099 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1100 | int ret; |
| 1101 | u8 num; |
| 1102 | |
| 1103 | mutex_lock(&opts->lock); |
| 1104 | if (opts->refcnt) { |
| 1105 | ret = -EBUSY; |
| 1106 | goto end; |
| 1107 | } |
| 1108 | |
| 1109 | ret = kstrtou8(page, 0, &num); |
| 1110 | if (ret) |
| 1111 | goto end; |
| 1112 | |
| 1113 | if (num > 15) { |
| 1114 | ret = -EINVAL; |
| 1115 | goto end; |
| 1116 | } |
| 1117 | |
| 1118 | opts->isoc_maxburst = num; |
| 1119 | ret = len; |
| 1120 | end: |
| 1121 | mutex_unlock(&opts->lock); |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1125 | CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1126 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1127 | static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page) |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1128 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1129 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1130 | int result; |
| 1131 | |
| 1132 | mutex_lock(&opts->lock); |
Asaf Vertz | 9fdd84d | 2015-01-21 11:06:34 +0200 | [diff] [blame] | 1133 | result = sprintf(page, "%u", opts->bulk_buflen); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1134 | mutex_unlock(&opts->lock); |
| 1135 | |
| 1136 | return result; |
| 1137 | } |
| 1138 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1139 | static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item, |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1140 | const char *page, size_t len) |
| 1141 | { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1142 | struct f_ss_opts *opts = to_f_ss_opts(item); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1143 | int ret; |
| 1144 | u32 num; |
| 1145 | |
| 1146 | mutex_lock(&opts->lock); |
| 1147 | if (opts->refcnt) { |
| 1148 | ret = -EBUSY; |
| 1149 | goto end; |
| 1150 | } |
| 1151 | |
| 1152 | ret = kstrtou32(page, 0, &num); |
| 1153 | if (ret) |
| 1154 | goto end; |
| 1155 | |
| 1156 | opts->bulk_buflen = num; |
| 1157 | ret = len; |
| 1158 | end: |
| 1159 | mutex_unlock(&opts->lock); |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1163 | CONFIGFS_ATTR(f_ss_opts_, bulk_buflen); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1164 | |
| 1165 | static struct configfs_attribute *ss_attrs[] = { |
Christoph Hellwig | 208e61a | 2015-10-03 15:32:46 +0200 | [diff] [blame] | 1166 | &f_ss_opts_attr_pattern, |
| 1167 | &f_ss_opts_attr_isoc_interval, |
| 1168 | &f_ss_opts_attr_isoc_maxpacket, |
| 1169 | &f_ss_opts_attr_isoc_mult, |
| 1170 | &f_ss_opts_attr_isoc_maxburst, |
| 1171 | &f_ss_opts_attr_bulk_buflen, |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1172 | NULL, |
| 1173 | }; |
| 1174 | |
| 1175 | static struct config_item_type ss_func_type = { |
| 1176 | .ct_item_ops = &ss_item_ops, |
| 1177 | .ct_attrs = ss_attrs, |
| 1178 | .ct_owner = THIS_MODULE, |
| 1179 | }; |
| 1180 | |
Andrzej Pietrasiewicz | 9890e33 | 2013-04-18 14:43:25 +0200 | [diff] [blame] | 1181 | static void source_sink_free_instance(struct usb_function_instance *fi) |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 1182 | { |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 1183 | struct f_ss_opts *ss_opts; |
| 1184 | |
| 1185 | ss_opts = container_of(fi, struct f_ss_opts, func_inst); |
| 1186 | kfree(ss_opts); |
Sebastian Andrzej Siewior | 544aca3 | 2012-12-23 21:09:57 +0100 | [diff] [blame] | 1187 | } |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 1188 | |
| 1189 | static struct usb_function_instance *source_sink_alloc_inst(void) |
| 1190 | { |
| 1191 | struct f_ss_opts *ss_opts; |
| 1192 | |
| 1193 | ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL); |
| 1194 | if (!ss_opts) |
| 1195 | return ERR_PTR(-ENOMEM); |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1196 | mutex_init(&ss_opts->lock); |
Andrzej Pietrasiewicz | 9890e33 | 2013-04-18 14:43:25 +0200 | [diff] [blame] | 1197 | ss_opts->func_inst.free_func_inst = source_sink_free_instance; |
Andrzej Pietrasiewicz | 25d8015 | 2013-11-07 08:41:28 +0100 | [diff] [blame] | 1198 | ss_opts->isoc_interval = GZERO_ISOC_INTERVAL; |
| 1199 | ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET; |
| 1200 | ss_opts->bulk_buflen = GZERO_BULK_BUFLEN; |
| 1201 | |
| 1202 | config_group_init_type_name(&ss_opts->func_inst.group, "", |
| 1203 | &ss_func_type); |
| 1204 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 1205 | return &ss_opts->func_inst; |
| 1206 | } |
| 1207 | DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst, |
| 1208 | source_sink_alloc_func); |
| 1209 | |
| 1210 | static int __init sslb_modinit(void) |
| 1211 | { |
| 1212 | int ret; |
| 1213 | |
| 1214 | ret = usb_function_register(&SourceSinkusb_func); |
| 1215 | if (ret) |
| 1216 | return ret; |
| 1217 | ret = lb_modinit(); |
| 1218 | if (ret) |
| 1219 | usb_function_unregister(&SourceSinkusb_func); |
| 1220 | return ret; |
| 1221 | } |
| 1222 | static void __exit sslb_modexit(void) |
| 1223 | { |
| 1224 | usb_function_unregister(&SourceSinkusb_func); |
| 1225 | lb_modexit(); |
| 1226 | } |
| 1227 | module_init(sslb_modinit); |
| 1228 | module_exit(sslb_modexit); |
| 1229 | |
| 1230 | MODULE_LICENSE("GPL"); |